JSON Decode into php array -
i using json square brackets , decode 2 multidimensional array.
this json:
"results" : [[ /* winner bracket */ [[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]], [[1,2], [3,4], [5,6], [7,8]], [[9,1], [8,2]], [[1,3]] ], [ /* loser bracket */ [[5,1], [1,2], [3,2], [6,9]], [[8,2], [1,2], [6,2], [1,3]], [[1,2], [3,1]], [[3,0], [1,9]], [[3,2]], [[4,2]] ], [ /* finals */ [[3,8], [1,2]], [[2,1]] ]]
and looking decode above type of php array shown below:
$winner_results = array ( array("match1",3,5), array("match2",2,4), array("match3",6,3), array("match4",2,3), array("match5",1,5), array("match6",5,3), array("match7",7,2), array("match8",1,2), array("match9",1,12), array("match10",3,4), array("match11",5,6), array("match12",7,8), array("match13",9,1), array("match14",8,2), array("match15",1,3) ); $loser_results = array ( array("match16",5,1), array("match17",1,2), array("match18",3,2), array("match19",6,9), array("match20",8,2), array("match21",1,2), array("match22",6,2), array("match23",1,3), array("match24",1,2), array("match25",3,1), array("match26",3,0), array("match27",1,9), array("match28",3,2), array("match29",4,2) ); $finals_results = array ( array("match30",3,8), array("match31",1,2), array("match32",2,1) );
and possible encode above php array exact same json format shown?
many help!
here's solution.
note: have used dynamic variable names ($$varname) create own arrays each group (winners, loosers, final).
<?php $results = '[[ [[3,5], [2,4], [6,3], [2,3], [1,5], [5,3], [7,2], [1,2]], [[1,2], [3,4], [5,6], [7,8]], [[9,1], [8,2]], [[1,3]] ], [ [[5,1], [1,2], [3,2], [6,9]], [[8,2], [1,2], [6,2], [1,3]], [[1,2], [3,1]], [[3,0], [1,9]], [[3,2]], [[4,2]] ], [ [[3,8], [1,2]], [[2,1]] ]]'; // these group names $names = array('winner_results','looser_results','final_results'); // first make php array out of json (object) string $resarr = json_decode($results); // create new array each group, re-organizing input array $resarr $cnt = 0; // counter use qualifying 'match' identifiers foreach($resarr $groupix => $group) { $arrname = $names[$groupix]; $$arrname = array(); // create empty array group foreach($group $items) { foreach($items $item) { // add data (array) group array $cnt++; array_push($$arrname,array('match'.$cnt,$item[0],$item[1])); } } } // , display created arrays foreach ($names $arr) { echo '<h3>'.$arr.'</h3><pre>'; print_r($$arr); echo '</pre>'; } ?>
Comments
Post a Comment