0

Hello I can't figure out how to loop through this json encoded array and for each object, get all its values. I need each value as a variable for itself.

echo json_encode($formulars);

This is what i get when i echo it out

[{"project_name":"polle","type":"support","title":"vi","reason":"prover","solution":"igen","comments":"okay ","date_stamp":"2013-08-20 14:06:37","used_time":132},{"project_name":"dolla","type":"support","title":"lolol","reason":"skl","solution":"dskal","comments":"kflafda ","date_stamp":"2013-08-20 14:11:36","used_time":210},{"project_name":"polle","type":"fejl","title":"lol","reason":"aksdl","solution":"fdjks","comments":"djsks ","date_stamp":"2013-08-20 14:13:27","used_time":1230}]

I have tried this piece of code and I managed to get out the project_name from the first object and that's it:

foreach ($formulars as $current => $project_name) {
        $project_name['project_name'];

    }

So is there any way i can get all the variables for each object in my array instead of just the project_name?

Like this:

foreach ($formulars as $current){ 
    $projectName = $current['project_name'];
    $type = $current['type'];
    $reason = $current['reason'];

}

Thanks in advance

3
  • 1
    try json_decode() then you can access it. Commented Aug 22, 2013 at 16:02
  • The "like this:" code seems sufficient. Why didn't it work? What happened? Commented Aug 22, 2013 at 16:03
  • Oh god mario I actually didn't even test out the "like this" code I just typed it out fast. I just tried it and it worked. I feel so stupid hehe. Thank you though ! Commented Aug 23, 2013 at 7:25

3 Answers 3

1

Seems like you have an objects inside an array. So you will need to loop through the array and get each object. Just JSON_DECODE your encoded string like below.

Perhaps:

$data = json_decode($formulars,true);
/* Since it's only one object inside the array, you could just select element zero, but I wil loop*/

//You should now be able to do this
foreach ($data as $current){ 
    $projectName = $current['project_name'];
    $type = $current['type'];
    $reason = $current['reason'];

 }

The reason I loop is because there is a object inside an array(Javascript way I think).

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

2 Comments

I have tried to use json_decode but it wont let me. I get this message: Warning: json_decode() expects parameter 1 to be string, array given
@Alex.M.K Oh... Then it means you can skip the decoding step because it is no longer a json string. It's an array(probably decoded it somewhere). You can just skip to the foreach step. Or use element zero in this case. But rather loop through it.
1

Use json_decode to convert the json object to an array; then use foreach to loop through the array. That should work.

<?php
    $arr_json = json_decode($formulars);
    foreach($arr_json as $key => $value)
        //Code to perform required actions
?>

This should give you some ideas.

Comments

0

Use json_decode (with TRUE for getting an associative array)to convert your JSON object to an associative array. After that, you can use a foreach loop to traverse through your multidimensional array and print the required values.

Code:

$json = json_decode($string, true);

foreach ($json as $key => $value) {
  foreach($value as $key2 => $value2) {
    echo $value2."\n";
  }
}

Working Demo!

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.