These are two functions to manipulate PHP objects.
The first function is similar to array_merge function in that it merges two objects based on an index.
The second function in_object also acts like in_array to which it checks if a certain index exists in an array at a specific value.
Hopefully someone finds it helpful.
<?php
// Object merge and in Object functions
function object_merge($objectFrom,$objectTo,$indexUse)
{
$objectReturn = array();
foreach($objectFrom as $curObject)
{
$index = in_object($indexUse,$curObject->$indexUse,$objectTo,true);
if(is_int($index))
{
foreach($objectTo[$index] as $indexName => $indexValue)
{
$curObject->$indexName = $indexValue;
}
}
$objectReturn[] = (object) $curObject;
}
return $objectReturn;
}
function in_object($searchFor,$searchValue,$objectSearch,$returnResult)
{
foreach($objectSearch as $index => $thisObject)
{
if($thisObject->$searchFor == $searchValue)
{
if($returnResult)
{
return $index;
} else {
return true;
}
}
}
return false;
}
// Usage
$obj1[0] = (object) array('catId' => 1, 'catName' => 'Name');
$obj1[1] = (object) array('catId' => 2, 'catName' => 'Name2');
$obj2[0] = (object) array('catId' => 1, 'catDesc' => 'Desc');
$obj2[1] = (object) array('catId' => 2, 'catDesc' => 'Desc2');
$obj_merged = object_merge($obj1,$obj2,'catId');
print_r($obj_merged);