0

I apologize for not being word-perfect in English.
I have this result from a foreach loop in php.
my file is jason.
Merging more value into one array

Array
(
    [777565] => Array
        (
            [0] => Array
                (
                    [0] => 777565-1
                    [1] => 777565-2
                 )
            [1] => Array
                (
                    [0] => 777565-3
                    [1] => 777565-4
                )
            [2] => Array
                (
                    [0] => 777565-5
                    [1] => 777565-6
                )
        )
    [777566] => Array
        (
            [0] => Array
                (
                    [0] => 777566-1
                    [1] => 777566-2
                )
            [1] => Array
                (
                    [0] => 777566-3
                    [1] => 777566-4
                )
            [2] => Array
                (
                    [0] => 777566-5
                    [1] => 777566-6
                )
        )
    )

but, I want Something like this:

Array
(
    [777565] => Array
        (
                    [0] => 777565-1
                    [1] => 777565-2
                    [2] => 777565-3
                    [3] => 777565-4
                    [4] => 777565-5
                    [5] => 777565-6
        )
    [777566] => Array
        (
                    [0] => 777566-1
                    [1] => 777566-2
                    [2] => 777566-3
                    [3] => 777566-4
                    [4] => 777566-5
                    [5] => 777566-6
        )
    )

I tried hard and searched the internet but I could not find any way. Of course, I have the ability to move it to the database first and then to the array, but I think there should be a faster way. What do you think? thanks for reply.

1 Answers1

1

If you have no problem looping through it and flatten the array according to your desire then you can try this:

$parent = 
Array
    (
    [777565] => Array
        (
            [0] => Array
                (
                    [0] => 777565-1
                    [1] => 777565-2
                 )
            [1] => Array
                (
                    [0] => 777565-3
                    [1] => 777565-4
                )
            [2] => Array
                (
                    [0] => 777565-5
                    [1] => 777565-6
                )
        )
    [777566] => Array
        (
            [0] => Array
                (
                    [0] => 777566-1
                    [1] => 777566-2
                )
            [1] => Array
                (
                    [0] => 777566-3
                    [1] => 777566-4
                )
            [2] => Array
                (
                    [0] => 777566-5
                    [1] => 777566-6
                )
        )
    );

$length = count($parent);

$result=[];

for($i=0; $i<$length; $i++){
    for($j=0; $j<3; $j++){
        $l=0;
        for($k=0; $k<2; $k++){
            $result[777565+$i][$j][$l++] = $parent[777565+$i][$j][$k];
        }
    }
}
Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22