1

I was trying to parsing json file below from a link but I still can't figure it out about parsing and display it with foreach.

data: [
{
id: "1072",
nik: "013977",
status: "",
name: "RAKHMAT KUSNADI",
birthdate: "1983-10-21",
email: "[email protected]",
first_login: "0",
is_juri: "0",
what_juri: "",
categorized: "0",
back_stage: "0",
placement: [
{
rel_id: "1102",
employee_id: "1072",
department_id: "101",
dept: "Chip",
position_id: "1",
position: ""
}
],
profile_pics: "link"
},
{
id: "1069",
nik: "013377",
status: "",
name: "RENATA MARINGKA",
birthdate: "1987-05-20",
email: "",
first_login: "1",
is_juri: "0",
what_juri: "",
categorized: "0",
back_stage: "0",
placement: [
{
rel_id: "1099",
employee_id: "1069",
department_id: "101",
dept: "Chip",
position_id: "1",
position: ""
}
],
profile_pics: "link"
},
]
}

I want to display name and profile_pics where department id is 101.

Does anybody know how to parse it with foreach?

1
  • Make sure your JSON validates. jsonlint.com Commented Oct 20, 2014 at 13:33

6 Answers 6

5

Reinventing the wheel, are we? Why not simply use:

$jsonObj = json_decode($jsonString);//returns stdClass instance, just an object
$jsonArr = json_decode($jsonString, true);//converts object to associative array

Read more on json_decode here... It's quite easy to use, really

If you decode the data to an array, you could loop through the data like so

while($item = array_shift($jsonArr))
{
    foreach ($item as $key => $value)
    {
        echo $key.' => '.$value."\n";
    }
}

Or simply use any old for/foreach loop on an object, its a traversable object anyway (though it doesn't implement the Traversable interface)

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

Comments

2

First step is to convert to an array

$data = json_decode($json);

Once you've got the array, you can then loop through it and check the values

$keepers = array();
foreach ($data as $item) {
  if ($item->placement->department_id == 101) {
    $keepers[] = $item;
  }
}

Comments

2

Use json_decode

  $arr = json_decode($jsonstring, true);

then use foreach loop

foreach($arr as $val) {
    if($val['placement']['department_id'] == "101") {
       //display what u want
    }
}

Comments

0

Probably something like:

$data = json_decode($your_json);

foreach($data as $object) {
    echo 'This is the name: ' . $object->name . PHP_EOL ;
}

Comments

0
$data = json_decode($json, true)

Then whatever info you want out you get out with the foreach loop

foreach ($data->id as $id)
{
echo $id;
}

With that you can set any variable like $data->nik as $nik or whatever you want then echo them back

Comments

0

usr this

foreach ($data->id as $id)
{
echo $id;
}

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.