使用JSON和Simple XML

时间:2016-11-10 11:37:22

标签: php json simplexml associative-array

我读取了一个XML文件,想要从输出数组中获取值。这是编写数组的代码:

$xml = simplexml_load_file('http://www.xyz.be/meteo'); 
$json_string = json_encode($xml);
$result_array = json_decode($json_string, TRUE);

输出数组具有以下结构:

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
                    (
                       ...

我经常尝试很多事情,但我找不到合适的解决方案。

**问题** 我怎么能把它们放在一个像这样的关联数组中:

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
    )
    ....
   )

我收到很多错误(通知):数组到字符串转换,未定义的偏移量:0,未定义的偏移量:1 ...

有谁知道解决方案?

提前致谢!

1 个答案:

答案 0 :(得分:0)

我想我找到了它。

也许不是最好的代码,但它有效。

method

给我结果:

$data = array();
$i = 0;

foreach($result_array['STATION'] as $station) { 
    $dag = 1;
       foreach($station['DAY'] as $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;
       }       
}