Simple and easy-to-understand solution could be as next.
The main idea – find index of required type
and then retrieve data from this index.
Once you’ve find your type
you will catch the index and break the loop:
$indd = ''; // possible array index with required type value
$type = 102; // type value
foreach($ar as $ind=>$arr){
if ($arr['type'] == $type) {
$indd = $ind;
break;
}
}
With catched index you can easy get rest of data:
if($indd){
echo $ar[$indd]['width'].PHP_EOL;
echo $ar[$indd]['height'].PHP_EOL;
echo $ar[$indd]['url'].PHP_EOL.PHP_EOL;
}
Outputs:
340
604
https://b.com
All in one function could be present like:
function arrayByType($arra, $type){
$indd = '';
foreach($arra as $ind=>$arr){
if ($arr['type'] == $type) {
$indd = $ind;
break;
}
}
if($indd){
return $arra[$indd];
}
}
Outputs:
Array
(
[type] => 102
[width] => 340
[height] => 604
[url] => https://b.com
)
NOTE: This solution works properly in case of unique type
value. Multiple appearances don’t supports.
1
solved How to Get Data from this PHP Array? [closed]