1

i have this json data

{"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}]
        }
    }
}

im still learning to understand... but could anyone point me on the right direction. I would like to achieve this using PHP the following...

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

2 Answers 2

2

Like this:

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

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

Obviously adapt the output to your needs but given your json that should fill out the values you want.

Sign up to request clarification or add additional context in comments.

Comments

0

You'll have to use json_decode, and then iterate through that array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.