I am using a foreach loop to echo names from my multi-dimensional array.
Sample Array:
$readJson = [
[
'id' => 78,
'name' => 'audi',
'sscat_id' => 59,
'date' => '0000-00-00 00:00:00'
],
[
'id' => 106,
'name' => 'apache',
'sscat_id' => 86,
'date' => '0000-00-00 00:00:00'
],
[
'id' => 16,
'name' => 'asia',
'sscat_id' => 62,
'date' => '0000-00-00 00:00:00'
]
];
I need to implement a condition whereby if the value of $_GET['b']
exists in the id
column of my array, I want to echo that subarray's name
value.
If $_GET['b']
does not exist in my array, I want to echo all of the name
values in the array.
The is my failing code:
foreach ($readJson as $key => $value){
if($_GET["b"] === $value["id"]){ // here is statement
echo $value["name"]; // I want to echo just this item not others
// maybe break; ?
} else {
echo $value["name"]; // echo all items
}
}
I guess I need something like break
but I know break
won't echo items after it.
Or if I get item index maybe I could echo it by index or id?