1

I want to convert a multi array in 2-D array. I have following result of multi array.

Array
(
[1] => Array
    (
        [0] => Array
            (
                [0] => Id
                [1] => Name
                [2] => Fname
                [3] => School
                [4] => Photo
            )

    )

[2] => Array
    (
        [0] => Array
            (
                [0] => 32
                [1] => kamal
                [2] => hjhbg
                [3] => hnp
                [4] => B612_16.jpg
            )

    )

[3] => Array
    (
        [0] => Array
            (
                [0] => 33
                [1] => dg
                [2] => fa
                [3] => f
                [4] => bg.jpg
            )

    )

[4] => Array
    (
        [0] => Array
            (
                [0] => 35
                [1] => mohit
                [2] => bc
                [3] => jhbvj
                [4] => B612.jpg
            )

    )

  )

Now I need to convert this array in below format.

Array
(
[0] => Array
    (
                [0] => Id
                [1] => Name
                [2] => Fname
                [3] => School
                [4] => Photo

    )

[1] => Array
    (
                [0] => 32
                [1] => kamal
                [2] => hjhbg
                [3] => hnp
                [4] => B612_16.jpg

    )

[2] => Array
    (
                [0] => 33
                [1] => dg
                [2] => fa
                [3] => f
                [4] => bg.jpg

    )

[3] => Array
    (
                [0] => 35
                [1] => mohit
                [2] => bc
                [3] => jhbvj
                [4] => B612.jpg
    )

    )
2

3 Answers 3

1

Try this

function array_to1d($a) {
    $out = array();
    foreach ($a as $b) {
        foreach ($b as $c) {
            if (isset($c)) {
                $out[] = $c;
            }
        }
    }
    return $out;
}
    echo "<pre>"; print_r(array_to1d($array)); // $array your array name
Sign up to request clarification or add additional context in comments.

Comments

0

The shortest solution would be using array_walk() here:

array_walk($array, function(&$v) {
    $v = $v[0];
});

Comments

0

You just have to do

$two_d_array = array_values($three_d_array);

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.