Jump to content

Recommended Posts

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? 

Link to comment
https://forums.phpfreaks.com/topic/293501-array-confusion/
Share on other sites


$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
)

)

)
*/
Link to comment
https://forums.phpfreaks.com/topic/293501-array-confusion/#findComment-1501068
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/293501-array-confusion/#findComment-1501080
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.