PHP merge arrays if they have the same key value -


i have array of associative arrays.

$array = [     ['report_date' => 'date', 'name' => 'name'],      ['report_date' => 'date', 'color' => 'color'] ]; 

i want sort through array , if

$array[x]['report_date'] === $array[y]['report_date'] 

then need perform merge return in case:

$newarray = [['report_date'=>date,'name'=>name,'color'=>color]] 

of course need take account there may multiple arrays fulfill requirement , have merge them well.

i've tried couple of things, resulted in mapping , foreach merges took forever process , in end couldn't working.

any ideas?

you can create new array, indexed report_date , push values it:

$out=[]; foreach($array $subarray){     foreach($subarray $key=>$val){         $out[$subarray['report_date']][$key]=$val;     } }  var_dump($out); 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -