newsomjk Posted December 29, 2014 Share Posted December 29, 2014 I'm sure this is something simple that I'm just overlooking right now, but it's got me very frustrated and I just want a quick fix...I have 2 arrays, $data_old and $totals_old Let's say each looks like this (with the 123456 being different for both): Array ( [2014] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) ) When I merge the two, I get something like this: Array ( [2014] => Array ( [0] => Array ( [123456] => Array ( [EMPS] => 114350 ) ) [1] => Array ( [60] => Array ( [EMPS] => 1470 ) ) ) ) How can I avoid the [0] and [1] and just have those be [123456] and [60], without referencing the 123456 and 60? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 29, 2014 Share Posted December 29, 2014 $a[2014][123456]['EMPS']=114350; $b[2014][60]['EMPS']=1470; $c = $a; foreach ($b as $y => $ydata) { foreach ($ydata as $k => $v) { $c[$y][$k] = $v; } } echo '<pre>',print_r($c, true),'</pre>'; /* RESULT Array ( [2014] => Array ( [123456] => Array ( [EMPS] => 114350 ) [60] => Array ( [EMPS] => 1470 ) ) ) */ Quote Link to comment Share on other sites More sharing options...
requinix Posted December 29, 2014 Share Posted December 29, 2014 array_replace_recursive works. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 29, 2014 Share Posted December 29, 2014 We live and learn. I haven't used that one before. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 29, 2014 Share Posted December 29, 2014 I only found out about array_replace() and array_replace_recursive() not that long ago and realized that most of the times I wanted array_merge/merge_recursive() I wasn't actually trying to merge but to replace information (in an associative array). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.