Replace Object inside an Array in php Replace Object inside an Array in php codeigniter codeigniter

Replace Object inside an Array in php


There's no "built-in" function for this. You're gonna have to loop and compare each manually. array_map seems like an OK choice here:

$PredefinedResult = array_map(function($a) use($MainResult){    foreach($MainResult as $data){        if($a->EffectiveStatus === $data->EffectiveStatus){            return $data;        }    }    return $a;}, $PredefinedResult);

DEMO: http://codepad.viper-7.com/OHBQK8


Iterate through the array and manual compare the values as follows.

$res = array();foreach ($PredefineResult as $result){    foreach ($MainResult as $mresult){        if(($result->EffectiveStatus == $mresult->EffectiveStatus) && $mresult->RecordCount!=0){            $res[] = $mresult;        }else $res[] = $result;    }}print_r($res);