如何显示嵌套数组中的值

时间:2016-02-19 19:49:27

标签: php arrays nested

请帮我提取并显示此数组中的值..

当我执行print_r($array);

时,这是输出
Array
(
    [0] => SPD Object
        (
            [DRIVERNAME] => SPD Barry
            [STARTTIME] => 07:44
            [FINISHTIME] => 18:12
            [STOP] => 
            [SEQUENCENO] => 37
            [PLACENAME] => AMSTERDAM ZUIDOOST
            [ARRIVALTIME] => 17:32
        )

    [1] => SPD Object
        (
            [DRIVERNAME] => SPD Brady
            [STARTTIME] => 07:36
            [FINISHTIME] => 15:53
            [STOP] => 
            [SEQUENCENO] => 32
            [PLACENAME] => NIEUWEGEIN
            [ARRIVALTIME] => 15:30
        )

    [2] => SPD Object
        (
            [DRIVERNAME] => SPD Bram
            [STARTTIME] => 08:11
            [FINISHTIME] => 18:32
            [STOP] => 
            [SEQUENCENO] => 32
            [PLACENAME] => LAGE ZWALUWE
            [ARRIVALTIME] => 17:28
        )
)

我想要做的是,获取此驱动程序名称和序列号并回显它们。

更新:

我的完整代码可以在下面找到。

我得到一个包含这种东西的xml文件:

<TRIP>
    <DRIVERNAME>SPD Barry</DRIVERNAME>
    <STARTTIME>07:44</STARTTIME>
    <FINISHTIME>18:12</FINISHTIME>
    <STOP>
        <SEQUENCENO>1</SEQUENCENO>
        <PLACENAME>ROTTERDAM</PLACENAME>
        <ARRIVALTIME>08:30</ARRIVALTIME>
    </STOP>
</TRIP>

这是我的PHP文件,用于将数据收集到数组中。

<?php
class SPD {
    function SPD ($aa) {
        foreach ($aa as $k=>$v)
            $this->$k = $aa[$k];
    }
}

function readDatabase($filename) {
    // read the XML database of aminoacids
    $data = implode("", file($filename));
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);

    // loop through the structures
    foreach ($tags as $key=>$val) {
        if ($key == "TRIP") {
            $molranges = $val;
            // each contiguous pair of array entries are the 
            // lower and upper range for each molecule definition
            for ($i=0; $i < count($molranges); $i+=2) {
                $offset = $molranges[$i] + 1;
                $len = $molranges[$i + 1] - $offset;
                $tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return $tdb;
}

function parseMol($mvalues) {
    for ($i=0; $i < count($mvalues); $i++) {
        $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
    }
    return new SPD($mol);
}

$db = readDatabase("test.xml");

if(is_array($db)){
    foreach($db as $item) {
        echo $item->DRIVERNAME;
        echo $item->SEQUENCENO;
    }
}

?>

我想要做的是,回显驱动程序名称和序列号:)

1 个答案:

答案 0 :(得分:0)

这应该这样做:

<?php

class SPD {
    function SPD($aa) {
        foreach ($aa as $k => $v)
            $this->$k = $aa[$k];
    }
}

function readDatabase($filename) {
    // read the XML database of aminoacids
    $data = implode("", file($filename));
    $parser = xml_parser_create();
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $data, $values, $tags);
    xml_parser_free($parser);

    // loop through the structures
    foreach ($tags as $key => $val) {
        if ($key == "TRIP") {
            $molranges = $val;
            // each contiguous pair of array entries are the 
            // lower and upper range for each molecule definition
            for ($i = 0; $i < count($molranges); $i+=2) {
                $offset = $molranges[$i] + 1;
                $len = $molranges[$i + 1] - $offset;
                $tdb[] = parseMol(array_slice($values, $offset, $len));
            }
        } else {
            continue;
        }
    }
    return $tdb;
}

function parseMol($mvalues) {
    for ($i = 0; $i < count($mvalues); $i++) {
        if(isset($mvalues[$i]["value"])) {
            $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
        }
    }
    return new SPD($mol);
}

$db = readDatabase("test.xml");

if (is_array($db)) {
    foreach ($db as $item) {
        echo 'ITEM 1 : ' . "\n<br/>";
        echo '---------------' . "\n<br/>";
        echo 'DRIVERNAME : ' . $item->DRIVERNAME . "\n<br/>";
        echo 'SEQUENCENO : ' . $item->SEQUENCENO . "\n\n<br/><br/>";
    }
}
?>