3

I am trying to seperate some value by key.

My current array like this

Array
(
    [name] => Array
        (
            [0] => test1
            [1] => test2
        )

    [type] => Array
        (
            [0] => image/jpeg
            [1] => image/jpeg
        )

    [tmp_name] => Array
        (
            [0] => D:\xampp5\tmp\php5F43.tmp
            [1] => D:\xampp5\tmp\php5F63.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 49293
            [1] => 20286
        )

)

My expected array will be like this

Array
( 
 [0] =>(
    [name] =>  test1

    [type] =>  image/jpeg

    [tmp_name] => D:\xampp5\tmp\php5F43.tmp

    [error] => 0

    [size] =>  49293
    )

[1] =>(
    [name] =>  test2

    [type] =>  image/jpeg

    [tmp_name] => D:\xampp5\tmp\php5F63.tmp

    [error] => 0

    [size] =>  20286
    )
)

For this I tried like this

$files = array();
$i = 0;
foreach ($array as $key => $value) {
    foreach ($value as $item) {
        $files[$i][] = $item;
        // echo '["'.$key.'"] - '.$item.'<br>';
        $i++;
    }
}

But it's just increasing array index not pushing under specific key.

Please suggest what I missed here

1
  • 1
    Get rid of $i, and instead make use of the second-level key. These 0 and 1 obviously are what needs to become the key on the first level in your new array. Commented Nov 28, 2019 at 9:19

3 Answers 3

7

Try this :

$result = [];

foreach ($array as $key => $subarray) {
    foreach ($subarray as $index => $data) {
        $result[$index][$key] = $data;
    }
}

Example here : link

Sign up to request clarification or add additional context in comments.

Comments

5

One way to achieve this result is to grab the keys from the first element of the array and use those to iterate through each element, using array_combine to join the keys from the top level with the values of that key for each element of the array:

$result = array();
$keys = array_keys($array);
foreach (array_keys(current($array)) as $key) {
    $result[$key] = array_combine($keys, array_column($array, $key));
}
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [name] => test1
            [type] => image/jpeg
            [tmp_name] => D:\xampp5\tmp\php5F43.tmp
            [error] => 0
            [size] => 49293
        )
    [1] => Array
        (
            [name] => test2
            [type] => image/jpeg
            [tmp_name] => D:\xampp5\tmp\php5F63.tmp
            [error] => 0
            [size] => 20286
        )
)

Demo on 3v4l.org

1 Comment

@LemonKazi thanks. While this makes full use of PHP's array functions, the answer you accepted will definitely perform better.
1
$images = array();

foreach ($array as $key => $subarray) {
    foreach ($subarray as $index => $data) {
        $images[$index][$key] = $data;
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.