1

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

    )


)
0

4 Answers 4

1

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', []);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Thanks Much @arkascha
0

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
        )

)

1 Comment

Thanks Much @Philipp
0

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
)

1 Comment

Thanks Much @Daniel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.