3

i have an array and want split them.may be 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 split to two array or if more

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

and

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

2 Answers 2

3

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
    )

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

4 Comments

You beat me to it! Here it is in a function: function recastArray($array) { $newArray = array(); foreach($array as $key=>$value) { foreach($value as $key2=>$value2){ $newArray[$key2][$key] = $value2; } } return $newArray; } echo "<pre>"; print_r(recastArray($array)); echo "</pre>";
@Difster Yes it is but i just make it in locally.. i didn't see that function.
I know, I was in the process of adapting a function I already had when you posted the answer, so I finished it and posted it anyway just to be helpful.
@alikarimi Please post your whole array..
1

I think this should be the solution:

foreach ($array1 as $value) {

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

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

Thanks!

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.