Thursday, 15 July 2010

Showing a search results page which has values from a multidimentional array in php -



Showing a search results page which has values from a multidimentional array in php -

i have array

array ( [availabletrips] => array ( [ac] => false [arrivaltime] => 1830 [availableseats] => 4 [boardingtimes] => array ( [bpid] => 460319 [bpname] => kalasipalyam [location] => kalasipalyam [prime] => false [time] => 1215 ) [bustype] => non a/c semisleeper (2+2) [bustypeid] => 110 [cancellationpolicy] => 0:8:100:0;8:24:50:0;24:-1:15:0 [departuretime] => 1215 [doj] => 2014-06-18t00:00:00+05:30 [droppingtimes] => array ( [bpid] => 559663 [bpname] => mananthavaadi [location] => mananthavaadi [prime] => false [time] => 390 ) [fares] => 650 [id] => 100102612640227182 [idproofrequired] => false [nonac] => true [operator] => 5127 [partialcancellationallowed] => false [routeid] => 100102600000227182 [seater] => true [sleeper] => false [travels] => sks travels [mticketenabled] => true ) )

which response api of bus ticket booking service.this search result of single bus got particular source , destination.for other source destination pairs,the result can like,if there more 1 bus on route.

array ( [availabletrips] => array ( [0] => array //1st bus in search result ( [ac] => true [arrivaltime] => 1845 [availableseats] => 28 [boardingtimes] => array ( [0] => array ( [bpid] => 46768 [bpname] => kalasipalyam main road [location] => kalasipalyam main road [prime] => false [time] => 1260 ) [1] => array ( [bpid] => 46774 [bpname] => christ university [location] => christ university [prime] => false [time] => 1280 ) [2] => array ( [bpid] => 46771 [bpname] => madiwala [location] => madiwala [prime] => true [time] => 1305 ) [3] => array ( [bpid] => 46776 [bpname] => electronic city toll gate [location] => electronic city toll gate [prime] => false [time] => 1330 ) [4] => array ( [bpid] => 46778 [bpname] => bommasandra post office [location] => bommasandra post office [prime] => false [time] => 1340 ) [5] => array ( [bpid] => 46779 [bpname] => attibele [location] => attibele [prime] => false [time] => 1350 ) [6] => array ( [bpid] => 46780 [bpname] => hosur [location] => hosur [prime] => false [time] => 1360 ) ) [bustype] => volvo multiaxle a/c seater semi sleeper (2+2) [bustypeid] => 101 [cancellationpolicy] => 0:12:100:0;12:-1:10:0 [departuretime] => 1305 [doj] => 2014-06-18t00:00:00+05:30 [droppingtimes] => array ( [0] => array ( [bpid] => 46886 [bpname] => edapally toll gate [location] => edapally toll gate [prime] => false [time] => 1825 ) [1] => array ( [bpid] => 46884 [bpname] => palarivattam [location] => palarivattam [prime] => false [time] => 1830 ) [2] => array ( [bpid] => 46882 [bpname] => vytla junction [location] => vytla junction [prime] => true [time] => 1845 ) [3] => array ( [bpid] => 46972 [bpname] => mg road [location] => mg road [prime] => false [time] => 1860 ) ) [fares] => 1000.00 [id] => 200855412640007772 [idproofrequired] => false [nonac] => false [operator] => 7478 [partialcancellationallowed] => true [routeid] => 200855400000007772 [seater] => true [sleeper] => false [travels] => shama transport [mticketenabled] => true ) [1] => array //2nd bus in search result ( [ac] => true [arrivaltime] => 1710 [availableseats] => 33 [boardingtimes] => array ( [0] => array ( [bpid] => 34106 [bpname] => kalasipalyam main road [location] => kalasipalyam main road [prime] => false [time] => 1080 ) [1] => array ( [bpid] => 34099 [bpname] => dairy circle [location] => dairy circle [prime] => false [time] => 1095 ) [2] => array ( [bpid] => 34098 [bpname] => christ university [location] => christ university [prime] => false [time] => 1100 ) [3] => array ( [bpid] => 34093 [bpname] => madiwala [location] => madiwala [prime] => true [time] => 1170 ) [4] => array ( [bpid] => 34103 [bpname] => electronic city toll gate [location] => electronic city toll gate [prime] => false [time] => 1185 ) [5] => array ( [bpid] => 34109 [bpname] => narayana hridayalaya [location] => narayana hridayalaya [prime] => false [time] => 1195 ) [6] => array ( [bpid] => 34095 [bpname] => attibele [location] => attibele [prime] => false [time] => 1200 ) [7] => array ( [bpid] => 34114 [bpname] => bommasandra post office [location] => bommasandra post office [prime] => false [time] => 1210 ) [8] => array ( [bpid] => 34112 [bpname] => hosur [location] => hosur [prime] => false [time] => 1215 ) ) [bustype] => volvo multiaxle a/c seater semi sleeper (2+2) [bustypeid] => 101 [cancellationpolicy] => 0:12:100:0;12:-1:10:0 [departuretime] => 1170 [doj] => 2014-06-18t00:00:00+05:30 [droppingtimes] => array ( [bpid] => 45746 [bpname] => vytala bye pass [location] => vyttila [prime] => true [time] => 1710 ) [fares] => 1100.00 [id] => 200855412640007290 [idproofrequired] => false [nonac] => false [operator] => 6708 [partialcancellationallowed] => true [routeid] => 200855400000007290 [seater] => true [sleeper] => false [travels] => atlas travels [mticketenabled] => true )

this goes on , on depending on number of results.

this part of code outputs results above.

$availablebuses = json_decode($availabletrips,true); echo "<pre>"; print_r($availablebuses); echo "</pre>";

now trying is,to loop , parse through array output results div like.

<h1>search results </h1> <div>bus name:<?php echo $ availablebuses['availabletrips']['travels'];?>boarding place:<?php echo $availablebuses['availabletrips']['boradingtimes']['location']</div>

the above like

**search results** bus name : sks travels boarding place : kalasipalyam

if there more results above repeated as

**search results** bus name : sks travels boarding place : kalasipalyam bus name : 1 travels boarding place : place 1 bus name : 2 travels boarding place : place 2 .......

im unable parse complex array properly

i have tried

$jsoniterator = new recursiveiteratoriterator( new recursivearrayiterator(json_decode($availabletrips, true)), recursiveiteratoriterator::self_first); foreach( $jsoniterator $output) { echo $output['travels']; echo $output['droppingtimes']['location']; }

but couldnt wanted.i want repeats div appropriate contents number of bus increase.

**i know can dumb questions around,but im finding hard through nor google did helped.if here help me of great help

thanks.

please excuse long question,didnt find way explain this.

given next array :

$availablebuses = array ( "availabletrips" => array ( "0" => array //1st bus in search result ( "ac" => "true", "arrivaltime" => "1845", "availableseats" => "28", "boardingtimes" => array ( "0" => array ( "bpid" => "46768", "bpname" => "kalasipalyam main road", "location" => "kalasipalyam main road", "prime" => "false", "time" => "1260" ), "1" => array ( "bpid" => "46774", "bpname" => "christ university", "location" => "christ university", "prime" => "false", "time" => "1280" ), "2" => array ( "bpid" => "46771", "bpname" => "madiwala", "location" => "madiwala", "prime" => "true", "time" => "1305" ), "3" => array ( "bpid" => "46776", "bpname" => "electronic city toll gate", "location" => "electronic city toll gate", "prime" => "false", "time" => "1330" ), "4" => array ( "bpid" => "46778", "bpname" => "bommasandra post office", "location" => "bommasandra post office", "prime" => "false", "time" => "1340" ), "5" => array ( "bpid" => "46779", "bpname" => "attibele", "location" => "attibele", "prime" => "false", "time" => "1350" ), "6" => array ( "bpid" => "46780", "bpname" => "hosur", "location" => "hosur", "prime" => "false", "time" => "1360" ), ), "bustype" => "volvo multiaxle a/c seater semi sleeper (2+2)", "bustypeid" => "101", "cancellationpolicy" => "0:12:100:0;12:-1:10:0", "departuretime" => "1305", "doj" => "2014-06-18t00:00:00+05:30", "droppingtimes" => array ( "0" => array ( "bpid" => "46886", "bpname" => "edapally toll gate", "location" => "edapally toll gate", "prime" => "false", "time" => "1825", ), "1" => array ( "bpid" => "46884", "bpname" => "palarivattam", "location" => "palarivattam", "prime" => "false", "time" => "1830" ), "2" => array ( "bpid" => "46882", "bpname" => "vytla junction", "location" => "vytla junction", "prime" => "true", "time" => "1845" ), "3" => array ( "bpid" => "46972", "bpname" => "mg road", "location" => "mg road", "prime" => "false", "time" => "1860" ) ), "fares" => "1000.00", "id" => "200855412640007772", "idproofrequired" => "false", "nonac" => "false", "operator" => "7478", "partialcancellationallowed" => "true", "routeid" => "200855400000007772", "seater" => "true", "sleeper" => "false", "travels" => "shama transport", "mticketenabled" => "true" ), "1" => array //2nd bus in search result ( "ac" => "true", "arrivaltime" => "1710", "availableseats" => "33", "boardingtimes" => array ( "0" => array ( "bpid" => "34106", "bpname" => "kalasipalyam main road", "location" => "kalasipalyam main road", "prime" => "false", "time" => "1080", ), "1" => array ( "bpid" => "34099", "bpname" => "dairy circle", "location" => "dairy circle", "prime" => "false", "time" => "1095" ), "2" => array ( "bpid" => "34098", "bpname" => "christ university", "location" => "christ university", "prime" => "false", "time" => "1100" ), "3" => array ( "bpid" => "34093", "bpname" => "madiwala", "location" => "madiwala", "prime" => "true", "time" => "1170" ), "4" => array ( "bpid" => "34103", "bpname" => "electronic city toll gate", "location" => "electronic city toll gate", "prime" => "false", "time" => "1185" ), "5" => array ( "bpid" => "34109", "bpname" => "narayana hridayalaya", "location" => "narayana hridayalaya", "prime" => "false", "time" => "1195" ), "6" => array ( "bpid" => "34095", "bpname" => "attibele", "location" => "attibele", "prime" => "false", "time" => "1200" ), "7" => array ( "bpid" => "34114", "bpname" => "bommasandra post office", "location" => "bommasandra post office", "prime" => "false", "time" => "1210" ), "8" => array ( "bpid" => "34112", "bpname" => "hosur", "location" => "hosur", "prime" => "false", "time" => "1215" ) ), "bustype" => "volvo multiaxle a/c seater semi sleeper (2+2)", "bustypeid" => "101", "cancellationpolicy" => "0:12:100:0;12:-1:10:0", "departuretime" => "1170", "doj" => "2014-06-18t00:00:00+05:30", "droppingtimes" => array ( "bpid" => "45746", "bpname" => "vytala bye pass", "location" => "vyttila", "prime" => "true", "time" => "1710" ), "fares" => "1100.00", "id" => "200855412640007290", "idproofrequired" => "false", "nonac" => "false", "operator" => "6708", "partialcancellationallowed" => "true", "routeid" => "200855400000007290", "seater" => "true", "sleeper" => "false", "travels" => "atlas travels", "mticketenabled" => "true" ) ) );

you want doing :

$workingarrayforbuses = array(); if(isset($availablebuses['availabletrips']['ac'])){ //one result $workingarrayforbuses[] = $availablebuses['availabletrips']; }else{ //multiple results $workingarrayforbuses = $availablebuses['availabletrips']; } echo '<h1>search results</h1>'; foreach($workingarrayforbuses $availablebuses){ echo '<div>'; echo 'bus name : '.$availablebuses['travels'].'<br/>'; $workingarrayforboardings = array(); if(isset($availablebuses['boardingtimes']['bpid'])){ //one result $workingarrayforboardings[] = $availablebuses['boardingtimes']; }else{ //multiple results $workingarrayforboardings = $availablebuses['boardingtimes']; } echo 'boarding place(s) :<br/>'; foreach($workingarrayforboardings $boardingtimes){ echo '- '.$boardingtimes['location'].'<br/>'; } echo '</div>'; }

that output :

bus name : shama transport boarding place(s) : - kalasipalyam main road - christ university - madiwala - electronic city toll gate - bommasandra post office - attibele - hosur bus name : atlas travels boarding place(s) : - kalasipalyam main road - dairy circle - christ university - madiwala - electronic city toll gate - narayana hridayalaya - attibele - bommasandra post office - hosur

well, guess need update want.

note : problem there is, way handle single result , multiple results homecoming array, problem beingness homecoming array 1 result :

array ( 'ac' => true ... )

instead of being

array ( [0] => array( 'ac' => true ... ) )

not satisfied trick used handle this, don't know how another(better) way. if got idea, know !

hope helps.

php arrays json foreach

No comments:

Post a Comment