-4

Does anyone know how I can loop this JSON array in PHP? I just tried, but it didn't work

[{
   //
    "data": [{
            "id": "1",
            "name": "Tiger Nixon",
            "position": "System Architect",
            "salary": "$320,800",
            "start_date": "2011/04/25",
            "office": "Edinburgh",
            "extn": "5421"
        },
        {
            "id": "2",
            "name": "Garrett Winters",
            "position": "Accountant",
            "salary": "$170,750",
            "start_date": "2011/07/25",
            "office": "Tokyo",
            "extn": "8422"
        },
        {
            "id": "3",
            "name": "Ashton Cox",
            "position": "Junior Technical Author",
            "salary": "$86,000",
            "start_date": "2009/01/12",
            "office": "San Francisco",
            "extn": "1562"
        },
        {
            "id": "4",
            "name": "Cedric Kelly",
            "position": "Senior Javascript Developer",
            "salary": "$433,060",
            "start_date": "2012/03/29",
            "office": "Edinburgh",
            "extn": "6224"
        }
    ]
}]
4

3 Answers 3

1

You should convert json to array then for each json convert object to array

$json = "current Json data";
$json = json_decode($json); //convert json to array
$json = $json[0]->data; //get all data

foreach ($json as $key => $value) {
    $value = get_object_vars($value); // convert object to array
    print_r($value)
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's right I checked it and working, can you try to as per my script?
0

First, decode your JSON into a PHP array, then you can loop through it as normal.

$arrayItems = json_decode($jsonString)

foreach ($arrayItems as $item){
 // do something
}

Comments

0

You can use following code to iterate json format:

$jsonData = json_decode($data);
        foreach($jsonData as $jsonDataKey => $jsonDataValue){
            foreach($jsonDataValue as $jsonArrayKey => $jsonArrayValue){
                echo $jsonArrayValue['id'];
                echo $jsonArrayValue['name'];
                echo $jsonArrayValue['position'];
            }
        }

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.