Created
March 21, 2014 17:23
-
-
Save AndreBaumeier/9691257 to your computer and use it in GitHub Desktop.
change the value of an associative array attached to an object by any array key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('LANGUAGE_NONE', 'und'); | |
function _update_array_value($object, $field, $key, $value, $lang = LANGUAGE_NONE) { | |
foreach ($object->{$field}[$lang] as &$sub) { | |
if (isset($sub[$key]) && $sub[$key] == $value[$key]) { | |
$sub = $value; | |
} | |
} | |
} | |
$node = new stdClass; | |
$node->field_entityreference[LANGUAGE_NONE] = array( | |
array('target_id'=>1), | |
array('target_id'=>2), | |
array('target_id'=>3), | |
array('target_id'=>4), | |
array('target_id'=>250), | |
array('target_id'=>262), | |
); | |
$key = 'target_id'; | |
$value = array( | |
'target_id' => 250, | |
'date' => '2014-03-21 18:13:20', | |
); | |
_update_array_value($node, 'field_entityreference', $key, $value); | |
print '<pre>'; | |
print_r($node); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment