dougai3418 2014-06-25 16:43
浏览 21
已采纳

比较两个多维对象数组

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;
}
  • 写回答

1条回答 默认 最新

  • douliu7929 2014-06-25 16:51
    关注

    To access this std class object you need to access it like this $Array2->ID you are accessing it like an array therefore this error pops up.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line