Created
March 21, 2014 17:51
-
-
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.
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'); | |
/** | |
* 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