0

Hi all,

I am aiming to create an array with the same structure as this:

Array
(
    [798D25C0DEABD] => Array
        (
            [quantity] => 1
        )

    [40B2B0FA3D222] => Array
        (
            [quantity] => 1
        )

)

The code I am using is therefore:

for($i = 0; $i < count($response); $i++)
{
    $ph[] = array($response[$i] => array('quantity' => $iteminfo[$i][1]));
}

However, this outputs an array that looks like this:

Array
(
    [0] => Array
        (
            [EB9A045DF3F11] => Array
                (
                    [quantity] => 1
                )

        )

    [1] => Array
        (
            [79EA2FC5287C0] => Array
                (
                    [quantity] => 1
                )

        )

)

How do I get rid of the incremental sub-arrays and keep everything under the one? Using $ph = on its own obviously doesn't work since it only stores the last array in the for statement. Can someone help?

5
Contributors
6
Replies
29
Views
2 Years
Discussion Span
Last Post by James_43
0

Ah, I discovered a new function!

$ph = call_user_func_array('array_merge', $ph);

Compacted everything nicely. Very slow though, so if someone has a better solution please let me know :)

2

Why don't you do something like this?

 for($i = 0; $i < count($response); $i++) {
     $ph[$response[$i]] = array('quantity' => $iteminfo[$i][1]);
 }
Votes + Comments
Ah, that's awesome. THanks so much!
0

Hi,

Try not to use for loops to iterate an array. I'm not sure what you are trying to achieve, but i've constructed similar data and looped through it. Hope it helps.

$array = array(
    '798D25C0DEABD' => array('quantity' => 1, 'price' => 20.5),
    '40B2B0FA3D222' => array('quantity' => 1, 'price' => 14.75)
);

//PHP 5.4
$array = [
    '798D25C0DEABD' => ['quantity' => 1, 'price' => 20.5],
    '40B2B0FA3D222' => ['quantity' => 1, 'price' => 14.75]
];

//print_r($array);

foreach($array as $key1 => $array2){
    echo $key1.'<br>';
    foreach($array2 as $key2 => $value){
        echo '---- '.$key2.' = '.$value.'<br>';
    }
}
1

You could also use array_combine, but methinks it'd be slower than a loop.

$array1 = ['798D25C0DEABD','863A5C0DEABD','1AA579DEAB','7383A76B0CC' ];
$array2 = [[2647,2],[3748,1],[458,2],[7890,3]]; //quantity being the second value in each subarray

$outputArray = array_combine($array1, array_map(function($item){return ['quantity'=>$item[1]];},$array2));

//EDIT
The loop version is about 7 times quicker :)
This is the ridiculous thing about array functions in PHP. They're supposed to make things convenient, but take a ridiculous time comparent to a loop!

$cnt=count($array1);
$output1=[];
for($i=0;$i<$cnt;$i++)
    $output1[$array1[$i]] = ['quantity'=>$array2[$i][1]];

As mentioend by invisal. :)

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.