Jeffrey Chan
Jeffrey Chan

Reputation: 93

list array function

array { 
[0]=> array { [0]=>  "1" [1]=>  "7" [2]=>  "5" [3]=>  "0" [4]=>  "0" } 
[1]=> array { [0]=>  "2" [1]=>  "3" [2]=>  "7" [3]=>  "0" [4]=>  "0" } 
[2]=> array { [0]=>  "3" [1]=>  "5" [2]=>  "10" [3]=>  "0" [4]=>  "0" } 
[3]=> array { [0]=>  "4" [1]=>  "11" [2]=>  "4" [3]=>  "0" [4]=>  "0" } 
[4]=> array { [0]=>  "5" [1]=>  "12" [2]=>  "9" [3]=>  "0" [4]=>  "0" } 
[5]=> array { [0]=>  "6" [1]=>  "6" [2]=>  "12" [3]=>  "0" [4]=>  "0" } 
[6]=> array { [0]=>  "7" [1]=>  "8" [2]=>  "6" [3]=>  "0" [4]=>  "0" } 
[7]=> array { [0]=>  "8" [1]=>  "0" [2]=>  "14" [3]=>  "0" [4]=>  "0" } 
[8]=> array { [0]=>  "9" [1]=>  "25" [2]=>  "8" [3]=>  "0" [4]=>  "0" } 
[9]=> array { [0]=>  "10" [1]=>  "30" [2]=>  "7" [3]=>  "0" [4]=>  "0" } 
}

i would like to ask how can i use list() function to list out

array{7,3,5,11,12,6,8,0,25,30}

thank you very much.

Upvotes: 2

Views: 566

Answers (3)

mickmackusa
mickmackusa

Reputation: 47903

The "modern" / functional way to isolate a single "column" of data from a two multidimensional array is to call array_column() and write the desired column's key as the second parameter.

Code: (Demo)

$array = [
    ["1", "7", "5", "0", "0"],
    ["2", "3", "7", "0", "0"],
    ["3", "5", "10", "0", "0"],
    ["4", "11", "4", "0", "0"],
    ["5", "12", "9", "0", "0"],
    ["6", "6", "12", "0", "0"],
    ["7", "8", "6", "0", "0"],
    ["8", "0", "14", "0", "0"],
    ["9", "25", "8", "0", "0"],
    ["10", "30", "7", "0", "0"],
];
var_export(array_column($array, 1));

Output:

array (
  0 => '7',
  1 => '3',
  2 => '5',
  3 => '11',
  4 => '12',
  5 => '6',
  6 => '8',
  7 => '0',
  8 => '25',
  9 => '30',
)

Upvotes: 0

Jimmy Sawczuk
Jimmy Sawczuk

Reputation: 13614

list() doesn't work that way, it assigns a one-dimensional array of size n to n variables. The way this data is structured, the only way I can see to extract that data is a foreach, as others have suggested.

Upvotes: 1

S L
S L

Reputation: 14318

$yourarray = array();

foreach($array as $arr)
{
    $yourarray[] = $arr[1];     
}

print_r($yourarray);

Upvotes: 3

Related Questions