Reputation: 9
"markers": [
{
"status": "gone",
"title": "HOWRAH JN (HWH)",
"lat": "22.58405800",
"lng": "88.34097900"
},
{
"status": "gone",
"title": "ASANSOL JN (ASN)",
"lat": "23.69110100",
"lng": "86.97481200"
},
{
"status": "current",
"title": "JAMTARA (JMT)",
"lat": "23.95927400",
"lng": "86.80555300"
},
{
"status": "yettocome",
"title": "MADHUPUR JN (MDP)",
"lat": "24.27075300",
"lng": "86.64230300"
},
{
"status": "yettocome",
"title": "JASIDIH JN (JSME)",
"lat": "24.51479700",
"lng": "86.64453500"
},
{
"status": "yettocome",
"title": "JHAJHA (JAJ)",
"lat": "24.77917600",
"lng": "86.39957400"
}
]
I want to get the only first value of "title" after "current". For example, I need to echo "title": "MADHUPUR JN (MDP)" i.e the first value of "status": "yettocome" . Here Is my code:
<?php
for($i=0; $i<count($array['markers']); $i++) {
if($array['markers'] [$i]['status'] == "yettocome") {
echo $array['markers'][$i]['title'];
}
}
?>
Upvotes: 0
Views: 33
Reputation: 22837
The foreach loop will loop over all values of the array.
The break
statement will end the foreach
loop's execution when it's reached.
I, personally, find that foreach
loops keep code a little neater than for
loops (especially with your use case).
foreach($array['markers'] as $marker){
if($marker['status'] === 'yettocome') {
echo $marker['title'];
break;
}
}
Upvotes: 1
Reputation: 2014
You need to remove the whitespace in $array['markers'] [$i]['status']
between ['markers']
and [$i]
.
Upvotes: 0