Skip to content

Instantly share code, notes, and snippets.

@AndreBaumeier
Created March 21, 2014 17:51
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/9691758 to your computer and use it in GitHub Desktop.
Save AndreBaumeier/9691758 to your computer and use it in GitHub Desktop.
Update an associatvie array attached to an object by one of it's keys.
<?php
define('LANGUAGE_NONE', 'und');
/**
* Update an associatvie array attached to an object by one of it's keys.
*
* @param $object object to work on
* @param string $field attribute used of object
* @param string $key key to be used of value array
* @param array $value
* @param string $lang
* @param int $limit
* if 0 no limit will be applied.
* @param $insert if true and no value is found, insert the value
* @return count of updated values
*/
function _update_array_value($object, $field, $key, $value, $lang = LANGUAGE_NONE, $limit = 1, $insert = FALSE) {
$count = 0;
foreach ($object->{$field}[$lang] as &$sub) {
if (isset($sub[$key]) && $sub[$key] == $value[$key]) {
$sub = $value;
$count++;
if ($limit > 0 && $count >= $limit) {
return $count;
}
}
}
if ($insert) {
$object->{$field}[$lang][] = $value;
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment