1
$multidimensionalArray = [[false,true,false], 
 [false,false,true], 
 [true,false,false], 
 [false,true,true]];

I know there is array_column function if I want to create new arrays from multidimensional array's columns. But I want to create new arrays from every row. What is the easiest way for this?

Result will be like this:

$array1 = [false,true,false];
$array2 = [false,false,true];
$array3 = [true,false,false];
$array4 = [false,true,true];
1
  • 1
    if you know that the array always contains 4 rows: list($ar1, $ar2, $ar3, $ar4) = $multidimensionalArray; Commented Jun 11, 2017 at 6:41

2 Answers 2

2
extract($multidimensionalArray, EXTR_PREFIX_ALL, 'array');

You will get:

$array_0 = [false,true,false];
$array_1 = [false,false,true];
$array_2 = [true,false,false];
$array_3 = [false,true,true];
Sign up to request clarification or add additional context in comments.

1 Comment

Great! I was looking for this kind of solution.
0

You can use this:

<?php

$multidimensionalArray = [[false,true,false], 
 [false,false,true], 
 [true,false,false], 
 [false,true,true]];


for($i = 1; $i < count($multidimensionalArray); $i++){
    ${'array' . $i} = $multidimensionalArray[$i];
}

var_dump($array1);
var_dump($array2);
var_dump($array3);
var_dump($array4);

1 Comment

Thank u... only I changed $i starting point... for ($i = 0; $i < count($multidimensionalArray); $i++) { ${'array' . ($i+1)} = $multidimensionalArray[$i]; }

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.