Does anyone know how to extract arrays that of a certain type from a larger array? For example, consider these results from a var_dump():
[0]=>
array(5) {
["post"]=>
string(60) "Paris"
["capacity"]=>
string(5) "58515"
["region"]=>
string(60) "WHA"
["seats"]=>
string(2) "55"
["growth"]=>
string(4) "3880"
}
[1]=>
array(5) {
["post"]=>
string(60) "Tel Aviv"
["capacity"]=>
string(6) "530910"
["region"]=>
string(60) "NEA"
["seats"]=>
string(2) "65"
["growth"]=>
string(4) "3267"
}
[2]=>
array(5) {
["post"]=>
string(60) "Paris"
["capacity"]=>
string(6) "962115"
["region"]=>
string(60) "EUR"
["seats"]=>
string(2) "11"
["growth"]=>
string(4) "2660"
}
I am sure this may seem simple to some that live in the 5th dimension, but I am having a devil of a time extracting arrays where region = "WHA" within my foreach() to try and build a separate array with just the WHA data.
foreach($data as $keys => $datums){
//Planning to build JSON string
if($datums['region'] == "WHA"){
//Some processing
}
}
The result should look like this:
[0]=>
array(5) {
["post"]=>
string(60) "Paris"
["capacity"]=>
string(5) "58515"
["region"]=>
string(60) "WHA"
["seats"]=>
string(2) "55"
["growth"]=>
string(4) "3880"
}
I have tried all kinds of different things like looking at array_filter that are just not working for me, and I am just concerned about what the right way looks like. Any ideas? Much appreciated.
if($datums['region'] == "WHA"){ $result[] = $datums; }. Or also witharray_filter(), e.g.$result = array_filter($yourArray, function($v){return $v["region"] == "WHA";});(btw: I live in the 4th dimension :)