简体   繁体   中英

How to separated key values with same index in a new multidimensional array

I have below multidimensional array and need help to convert multidimensional in the specific formation

Array
(
    [0] => Array
        (
            [0] => 20
            [1] => 15
            [2] => 0
        )

    [1] => Array
        (
            [0] => 28
            [1] => 45
            [2] => 0
        )

    [2] => Array
        (
            [0] => 20
            [1] => 5
            [2] => 50
        )

    [3] => Array
        (
            [0] => 48
            [1] => 20
            [2] => 0
        )

)

and want to convert like this:

Array
(
    [0] => Array
        (
            [0] => 20
            [1] => 28
            [2] => 20
            [3] => 48
        )

    [1] => Array
        (
            [0] => 15
            [1] => 45
            [2] => 5
            [3] => 20
        )

    [2] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 50
            [3] => 0
        )
)

On the above output, I have separated key values with the same index in a new array.

Is it possible?

You can use array_map function

$result = array_map(null, ...$array);

demo

Here is one of the solutions:

<?php

$array =
[
    0 =>
        [
            0 => 20,
            1 => 15,
            2 => 0
        ],

    1 =>
        [
            0 => 28,
            1 => 45,
            2 => 0
        ],

    2 =>
        [
            0 => 20,
            1 => 5,
            2 => 50
        ],

    3 =>
        [
            0 => 48,
            1 => 20,
            2 => 0
        ]
];

$result = [];
echo '<pre>';
foreach ($array as $key => $value) {
    for ($i = 0; $i<count($value); $i++) {
        $result[$i][] = $value[$i];
    }
}


var_dump($result);

Result:

array(3) {
  [0]=>
  array(4) {
    [0]=>
    int(20)
    [1]=>
    int(28)
    [2]=>
    int(20)
    [3]=>
    int(48)
  }
  [1]=>
  array(4) {
    [0]=>
    int(15)
    [1]=>
    int(45)
    [2]=>
    int(5)
    [3]=>
    int(20)
  }
  [2]=>
  array(4) {
    [0]=>
    int(0)
    [1]=>
    int(0)
    [2]=>
    int(50)
    [3]=>
    int(0)
  }
}

This code is working fine and doing what you are looking for, try to copy paste on the editor and run it.

<?php 
   $originalArray = Array(
         Array
            (
                 20,
                 15,
                 0
            ),

         Array
            (
                 28,
                 45,
                 0
            ),

         Array
            (
                 20,
                 5,
                 50
            ),

         Array
            (
                 48,
                 20,
                 0
            )

    ); //echo"<pre>";print_R($originalArray); 00 10 20 30 40 : 01,11,21,31 :  02,12,22,32,    
$newArray =array();
//echo count($originalArray);
//echo count($originalArray[1]);
$count = array_map('count', $originalArray);
$min = array_keys($count , max($count))[0];
$largest_arr = $originalArray[$min];

for($i=0;$i<count($largest_arr);$i++){
array_push($newArray,$originalArray[$i]);
    for($j=0;$j<count($originalArray);$j++){
echo $j."--".$i."---".count($originalArray)."----".$originalArray[$j][$i]."<br>";
        array_push($newArray[$i],$originalArray[$j][$i]);
    }
}
echo "<pre>";print_R($newArray);
?>

I have found the solution from the PHP - how to flip the rows and columns of a 2D array post

$out = array();
foreach ($array as  $rowkey => $row) {
    foreach($row as $colkey => $col){
        $out[$colkey][$rowkey]=$col;
    }
}

This works perfectly for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM