wmintz
wmintz

Reputation: 13

combining php array on the right level when array_merge isn't working correctly

I am working inside a php application whose classes and namespaces are causing an issue where

    $array=array_merge($a,$b)

gives me

Array


(
    [0] => Array
        (
        [blue shoes] => 1464873
        [white shoes] => 2079
        [red shoes] => 0.1419
        [pink shoes] => 115

    )

[1] => Array
    (
        [black dress shoes] => 527471
        [white dress shoes] => 42.5232

    )

)

*****Starting from that array how exactly would I get*****

Array


(
[0] => Array
    (
        [blue shoes] => 1464873
        [white shoes] => 2079
        [red shoes] => 0.1419
        [pink shoes] => 115
        [black dress shoes] => 527471
        [white dress shoes] => 42.5232

    )


)

Upvotes: 1

Views: 31

Answers (4)

Daniel
Daniel

Reputation: 4946

Your array is multidimensional containing array 0 and array 1. You can iterate that array and collect it into a new array.

$a = array(
    array("blue shoes" => 1464873, "white shoes" => 2079, "red shoes" => 0.1419, "pink shoes" => 115),
    array("black dress shoes" => 527471, "white dress shoes" => 42.5232)
);


$newarray = array();

foreach ($a as $key => $oldarray) {

    foreach ($oldarray as $shoe => $indexcount) {

        $newarray[$shoe] = $indexcount;
    }
}

print_r($newarray);

Result:

Array
(
    [blue shoes] => 1464873
    [white shoes] => 2079
    [red shoes] => 0.1419
    [pink shoes] => 115
    [black dress shoes] => 527471
    [white dress shoes] => 42.5232
)

Upvotes: 0

Philipp
Philipp

Reputation: 15629

Maybe you're just looking for array_replace_recursive - but that depends on your input arrays.

$a = [[
    'blue shoes' => 1464873,
    'white shoes' => 2079,
    'red shoes' => 0.1419,
    'pink shoes' => 115
]];
$b = [[
    'black dress shoes' => 527471,
    'white dress shoes' => 42.5232
]];

$c = array_replace_recursive($a, $b);
print_r($c);

That would print

Array (
    [0] => Array
        (
            [blue shoes] => 1464873
            [white shoes] => 2079
            [red shoes] => 0.1419
            [pink shoes] => 115
            [black dress shoes] => 527471
            [white dress shoes] => 42.5232
        )

)

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24156

the simplest fix is to change $array=array_merge($a,$b) to:

$array = array_merge(array_pop($a), array_pop($b));

but, if you have to work with array you provided, you can do something like this:

$array = array_reduce($array, 'array_merge', []);

Upvotes: 1

arkascha
arkascha

Reputation: 42935

This looks like your variables $a and $b do not contain flat arrays but arrays with a single element each that holds a flat array. So you want to merge the two first elements of those two variables, I'd say.

Take a look at this example:

<?php
$a = [
    [
        'blue shoes' => 1464873,
        'white shoes' => 2079,
        'red shoes' => 0.1419,
        'pink shoes' => 115
    ]
];
$b = [
    [
        'black dress shoes' => 527471,
        'white dress shoes' => 42.5232
    ]
];

print_r(array_merge($a, $b));
print_r(array_merge($a[0], $b[0]));

The output of above code obviously is:

Array
(
    [0] => Array
        (
            [blue shoes] => 1464873
            [white shoes] => 2079
            [red shoes] => 0.1419
            [pink shoes] => 115
        )

    [1] => Array
        (
            [black dress shoes] => 527471
            [white dress shoes] => 42.5232
        )

)
Array
(
    [blue shoes] => 1464873
    [white shoes] => 2079
    [red shoes] => 0.1419
    [pink shoes] => 115
    [black dress shoes] => 527471
    [white dress shoes] => 42.5232
)

This demonstrates the issue you face:

  • the merging as you attempted leads to exactly the result you posted.
  • merging the first elements of those arrays delivers the desired result.

Upvotes: 0

Related Questions