Looping thought multilevel array php

时间:2015-12-10 01:39:02

标签: php

I have an array in php that looks like this:

print_r($myArray);  

Array (
[0] => Array
    (
        [age] => 1
        [time] => 2
        [name] => james
        [size] => 12
        [hieght] => 13
    )

[1] => Array
    (
        [age] => 3
        [time] => 1
        [name] => tim
        [size] => 12
        [hieght] => 13
    )

[2] => Array
    (
        [age] => 1
        [time] => 2
        [name] => john
        [size] => 132
        [hieght] => 4
    )

[3] => Array
    (
        [age] => 1
        [time] => 2
        [name] => logan
        [size] => 12
        [hieght] => 11
    ) 
)

Im trying to loop though every item and save each "size" in a new array. I have looked into solutions for pulling out the "size" from each inner array but cant seem to get it right:

$all_sizes = array();
foreach($myArray as $value) {
    foreach($value as $key => $val) {
        if($key == "size") {

        } 
    }
}

I am new to PHP so im struggling on the proper syntax for this situation.

1 个答案:

答案 0 :(得分:2)

Remove the next foreach()

$all_sizes = array();
  foreach($myArray as $value => $getSize) {
    $all_sizes[] = $getSize['size'];
  }
print_r($all_sizes);