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.

4
  • Does this answer your question? How to Flatten a Multidimensional Array? Commented Apr 1, 2021 at 15:43
  • Unfortunately no. Commented Apr 1, 2021 at 15:55
  • how many parent array parameters like [777565] do you have? only two or more than that? Commented Apr 1, 2021 at 16:08
  • About a thousand Commented Apr 2, 2021 at 0:21

1 Answer 1

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

Comments

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.