在PHP中操纵多维数组

时间:2011-05-05 06:44:05

标签: php json multidimensional-array

我有这个json数据

{"results":{
    "result":{
        "count":182,
        "firsthit":1,
        "lasthit":182,
        "name":"Primary Schools",
        "schoolhit":[{
            "districtno":25,
            "name":"ADMIRALTY PRIMARY SCHOOL",
            "precinct":"Woodlands",
            "region":"North",
            "zipcode":738907},
            {
            "districtno":27,
            "name":"AHMAD IBRAHIM PRIMARY SCHOOL",
            "precinct":"Yishun",
            "region":"North",
            "zipcode":768643},
            {
            "districtno":20,
            "name":"AI TONG SCHOOL",
            "precinct":"Sin Ming",
            "region":"North",
            "zipcode":579646},
            {
            "districtno":19,
            "name":"ANCHOR GREEN PRIMARY SCHOOL",
            "precinct":"Sengkang",
            "region":"North",
            "zipcode":544969}]
        }
    }
}

我还在学习理解......但是有人能指出我正确的方向。我想用PHP实现这一点......

<select>
<?php print "<option value=\"$districtno\">$name</option>";?><br />
</select>

2 个答案:

答案 0 :(得分:2)

像这样:

$data = json_decode($json)->{'results'}->{'result'}->{'schoolhit'};

foreach ($data as $school) {
    echo "<option value=" . $school->{'districtno'} . ">" . $school->{'name'} . "</option>";
}

显然可以根据您的需要调整输出,但是如果你的json应该填写你想要的值。

答案 1 :(得分:0)

您必须使用json_decode,然后遍历该数组。