I’m working on a web application that takes its data from a web page using the Yahoo Query Language API to return a JSON array. I’ve hit a block in that sometimes, when there is only one “race” on the page, the array is setup differently and I can’t iterate through it in some circumstances. Let me explain in an example.
Array layout for a page with mutiple races
Array (
[div] => array (
[0] => array (
['venue_id'] = 02222
['venue_name'] = 'Hove'
['race_id'] = 9222
)
[1] => array (
['venue_id'] = 03333
['venue_name'] = 'Romford'
['race_id'] = 2442
)
//...and so on
)
)
Array layout for a page with just one race
Array (
[div] => array (
['venue_id'] = 02222
['venue_name'] = 'Hove'
['race_id'] = 9222
)
)
In the application, I’m currently using a simple foreach statement to iterate through the array. However, this obviously wont work with the second example and I need a workaround.
Example of foreach statement
foreach($result['div'] as $race) {
echo 'Venue ID: '.$race['venue_id'];
echo 'Venue Name: '.$race['venue_name'];
echo 'Race ID: '.$race['race_id'];
}
Any help would be massively appreciated!
Dan
I’d do this..
if(!isset($array[‘div’][0]))
$array[‘div’] = array($array[‘div’]);
This way I do not have to have two iteration methods.