I am struggling to compare 2 mufti-dimensional object arrays as follows and adding the difference to a empty array.
Array1
array(1) {
[0]=> object(stdClass)#393 (1) {
["ID"]=>
string(3) "433"
}
}
Array 2
array(1) {
[0]=> object(stdClass)#386 (1) {
["ID"]=>
string(3) "433"
}
}
So far I have used the following function:
function compare_arrays($Array1, $Array2){
$result = array();
foreach ($Array2 as $index => $newItem) {
foreach ($Array1 as $oldItem) {
if($Array2['ID'] === $oldItem['ID'])
{
$result[] = $index;
}
}
}
return $result;
}
I am getting the following error?
Fatal error: Cannot use object of type stdClass as array
Can somebody please guide me as where I am going wrong? Or if there is a different simpler approach?
FIX
Code fixed as per Adeel and Fabricator
Had to create a special class to create a blank object and then assign the value to it.
function compare_arrays($para1, $para2){
$result = array();
/*Special class to create a blank object*/
$data = new stdClass();
foreach ($para2 as $index => $newItem) {
foreach ($para1 as $oldItem) {
$test2 = $oldItem->ID;
if($newItem->ID === $oldItem->ID)
{
$data->ID = $newItem->ID;
$result[] = $data;
var_dump($results);
}
}
}
return $result;
}