php - Make associative array from array structure build with JSON and Simple XML -
i read xml file , want values output array. code write array:
$xml = simplexml_load_file('http://www.xyz.be/meteo'); $json_string = json_encode($xml); $result_array = json_decode($json_string, true);
the output array has following structure:
array ( [station] => array ( [0] => array ( [@attributes] => array ( [id] => 6407 [name] => kust ) [day] => array ( [0] => array ( [@attributes] => array ( [id] => 20161110 ) [tmax] => 10 [weather] => 11 [dd] => nw [ff] => 25 ) [1] => array ( [@attributes] => array ( [id] => 20161111 ) [tmax] => 8 [weather] => 2 [dd] => zo [ff] => 8 ) ) ) [1] => array ( [@attributes] => array ( [id] => 6479 [name] => kempen ) [day] => array ( [0] => array ( [@attributes] => array ( [id] => 20161110 ) [tmax] => 9 [weather] => 6 [dd] => zw [ff] => 11 ) [1] => array ( [@attributes] => array ( [id] => 20161111 ) [tmax] => 5 [weather] => 3 [dd] => no [ff] => 6 ) ) ) [2] => array ( [@attributes] => array ( [id] => 6476 [name] => ardennen ) [day] => array ( ...
i reayly tried lot of things don't find right solution.
** question ** how can put in associative array this:
array ( [0] => array ( ['stat_index '] => 0 ['stat_id'] => 6407 ['stat_name'] => kust ['day_index'] => 0 ['day_date'] => 20161110 ['day_tmax'] => 10 ['day_weather'] => 11 ['day_dd '] => nw ['day_ff '] => 25 ) [1] => array ( ['stat_index '] => 0 ['stat_id'] => 6407 ['stat_name'] => kust ['day_index'] => 1 ['day_date'] => 20161111 ['day_tmax'] => 8 ['day_weather'] => 2 ['day_dd '] => zo ['day_ff '] => 8 ) [2] => array ( ['stat_index '] => 0 ['stat_id'] => 6451 ['stat_name'] => centrum ['day_index'] => 0 ['day_date'] => 20161110 ['day_tmax'] => 10 ['day_weather'] => 11 ['day_dd '] => w ['day_ff '] => 16 ) .... )
i lot of errors (notices): array string conversion, undefined offset: 0, undefined offset: 1...
does know solution?
thanks in advance!
i think found it.
maybe not best code works.
$data = array(); $i = 0; foreach($result_array['station'] $station) { $dag = 1; foreach($station['day'] $day) { $data[$i]['id'] = $station['@attributes']['id']; $data[$i]['name'] = $station['@attributes']['name']; $data[$i]['datum'] = $day['@attributes']['id']; $data[$i]['dag'] = $dag; $data[$i]['tmax'] = $day['tmax']; $data[$i]['weather'] = $day['weather']; $data[$i]['dd'] = $day['dd']; $data[$i]['ff'] = $day['ff']; $i += 1; $dag += 1; } }
gives me result:
array ( [0] => array ( [id] => 6407 [name] => kust [datum] => 20161110 [dag] => 1 [tmax] => 10 [weather] => 11 [dd] => nw [ff] => 25 ) [1] => array ( [id] => 6407 [name] => kust [datum] => 20161111 [dag] => 2 [tmax] => 8 [weather] => 2 [dd] => zo [ff] => 8 ) [2] => array ( [id] => 6451
Comments
Post a Comment