php节点转换为多维数组


php node into multidimensional array

这是我迄今为止得到的一个脚本。

<?php 
 $xml= simplexml_load_file("http://meteo.arso.gov.si/uploads/probase/www/observ/surface/text/sl/observationAms_si_latest.xml");
  // print_r($xml);
$meteos = $xml->metData; 
$spot = array();
foreach ($meteos as $meteo) {
$spot[] = "$meteo->domain_title, $meteo->tsValid_issued, $meteo->t, $meteo->dd_icon, $meteo->ff_val, $meteo->ffmax_val, $meteo->msl, $meteo->rr_val, $meteo->rr_val";
}
print_r($spot);
    ?>

我得到的是这样的东西:

Array
(
    [0] => BILJE NOVA GORICA, 31.08.2013 21:00 CEST, 19.2, E, 1.5, 1.8, 1019, 0, 0
    [1] => BOHINJSKA CESNJICA, 31.08.2013 21:00 CEST, 14.4, , , , , , 
    [2] => BORST GORENJA VAS, 31.08.2013 21:00 CEST, 15.3, SW, 0.4, 1.3, 1020, 0, 0

我想要的是

Array
(
    [0] => BILJE NOVA GORICA
          [0] => 31.08.2013 21:00 CEST
          [1] =>19.2 
          [2] =>E
          [3] =>1.5
          [4] =>1.8 
          [5] =>1019

我该怎么做?此外,我想找到一个包含"ILIRSKA BISTRICA"的数组,这样我就可以将其写入数据库。

你不能真的那样做,但你有两个选择:

Array
(
    [BILJE NOVA GORICA]  => Array
    (
        [0] => 31.08.2013 21:00 CEST
        [1] =>19.2 
        ....
    )
)

Array
(
    [0] => Array
    (
        [name] => BILJE NOVA GORICA
        [values] => Array
        (
            [0] => 31.08.2013 21:00 CEST
            [1] =>19.2 
            ...
        )
    )
)

我推荐第一种方法,因为它更小,更容易使用。你可以通过简单的操作来设置你的阵列:

$spot[$meteo->domain_title] = array(
    $meteo->tsValid_issued,
    $meteo->t,
    $meteo->dd_icon,
    $meteo->ff_val,
    $meteo->ffmax_val,
    $meteo->msl,
    $meteo->rr_val,
    $meteo->rr_val
);

$meteo属性保存为数组项。

foreach ($meteos as $meteo) {
    $spot[$meteo->domain_title] = array(
        $meteo->tsValid_issued, 
        $meteo->t, 
        $meteo->dd_icon,     
        $meteo->ff_val, 
        $meteo->ffmax_val, 
        $meteo->msl, 
        $meteo->rr_val, 
        $meteo->rr_val
    );
}