Skip to content

Instantly share code, notes, and snippets.

@AndreBaumeier
Created March 21, 2014 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreBaumeier/9691257 to your computer and use it in GitHub Desktop.
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
<?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