Split nesting array for use in php

I have an array and want to split them .may by two, tree or more

array(
     name=>array(
                0=>asda.jpg,
                1=>kewj.jpg
                ),
     type=>array(
                0=>jpg,
                1=>jpg
                ),
     size=>array(
                0=>2133,
                1=>2222
                )
     )

      

I want to split into two arrays or more

array(
     name=>asd.jpg,
     type=>jpg,
     size=>2133
     )

      

and

array(
       name=>kewj.jpg,
       type=>jpg,
       size=>2222
      )

      

+3


source to share


2 answers


Here you can achieve it like this

Example

<?php
$arr = array(
'name' => array(
          0 => 'asda.jpg',
          1 => 'kewj.jpg'
        ),
'type' => array(
          0 => 'jpg',
          1 => 'jpg'
        ),
'size' => array(
          0 => '2133',
          1 => '2222'
        )
);
$arraySplit = array();
foreach($arr as $key => $value) {
    foreach($value as $key2 => $value2) {
            $arraySplit[$key2][$key] = $value2;
    }
}
echo "<pre>";
print_r($arraySplit);

      



Output

Array
(
[0] => Array
    (
        [name] => asda.jpg
        [type] => jpg
        [size] => 2133
    )

[1] => Array
    (
        [name] => kewj.jpg
        [type] => jpg
        [size] => 2222
    )

)

      

+3


source


I think this should be the solution:

foreach ($array1 as $value) {

    for($node=0;$node<count($value);$node++){

        $arr[$node][] = $value[$node];
    }    
}

      



Thank!

+1


source







All Articles