1

I have a site which I used JSON to save some data.

Here is how it saves data

{"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}}

Note: I know this is a poor way of doing it but I did it when I was not so vast!

The 1 is the user_id which changes according to the id of the user that takes an exam on the platform. The english, physics are the subjects this user took.

The maximum number of exam a user can take at a time is for so the json string will look like {"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"},"maths":{"grade":"7","time":"79"},"chemistry":{"grade":"3","time":"48"}}}

First I think this is the best way to save the result as a JSON string

[{"subject":"english","grade":"7","time":"79"}, {"subject":"physics", "grade":"3","time":"48"}}]

My problem is that I want to use PHP to work on the former on. I have done some few stripping of the string and I'm left with this {"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}

I tried this chunk

$JSONResult = json_decode($aScore->result);
foreach ($JSONResult as $subjectKey => $aSubject)
{
    foreach ($aSubject as $theResult)
    {
        $userResult = '
        Subject: **This is what I've not been able to get**
        Grade: '.$theResult->grade.'
        Time: '.$theResult->time.'
        ';
    }
}

I tried $aSubject->subjectKey to get the associative key value from the first foreach statement but it did not work

Any help?

Added: Please leave comments about the way the JSON string was stored. I'd love to learn.

Prodigy
  • 380
  • 3
  • 9

2 Answers2

2

You don't need the inner loop. Each subject is just a single object, not an array. And the subject key is just $subjectKey, it's not a part of the object.

$JSONResult = json_decode($aScore->result, true); // to get an associative array rather than objects
foreach ($JSONResult as $subjectKey => $aSubject) {
    $userResult = "
        Subject: $subjectKey
        Grade: {$aSubject['grade']}
        Time: {$aSubject['time']}
    ";
}

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You could use the second argument to json_decode!

It changes your $JSONResult from stdClass to an associative Array!

Which means you can do something like this:

$str = '{"1":{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}}';

$result = json_decode($str, true); // Put "true" in here!

echo "<pre>".print_r($result, true)."</pre>"; // Just for debugging!

Which would output this:

Array
(
    [1] => Array
    (
        [english] => Array
        (
            [grade] => 7
            [time] => 79
        )
        [physics] => Array
        (
            [grade] => 3
            [time] => 48
        )
    )
)

And in order to loop through it:

foreach ($result as $idx => $exams) {

    echo $exams['english']['grade']."<br>";
    echo $exams['physics']['grade']."<br>";

}

Which would output this:

7
3

Update

Without knowing the containing arrays data (Based on the example above)

$exams will be an Array (which could contain any sort of information):

Array
(
    [english] => Array
    (
        [grade] => 7
        [time] => 79
    )
    [physics] => Array
    (
        [grade] => 3
        [time] => 48
    )
)

If you want to loop through $exams:

foreach ($result as $idx => $exams) {

    foreach ($exams as $subject => $info) {

        echo $info['grade']."<br>";

    }

}

This would produce the same output as the above example, without needing to know a subject name!

Community
  • 1
  • 1
Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • This may fail from your final chunk because I don't know the exam subjects a user will pick so the $val['english']['grade'] should be something like $val[$subject]['grade]. What do you think? – Prodigy Oct 30 '15 at 22:29
  • I said I stripped the string and am left with this `{"english":{"grade":"7","time":"79"},"physics":{"grade":"3","time":"48"}}` how do I go about it from here not from the original one you set as your variable in your answer. – Prodigy Oct 30 '15 at 22:39