0

I am having the following json:

{
  "data": [
    {
      "id": "116376655202954/insights/page_impressions_organic/day", 
      "name": "page_impressions_organic", 
      "period": "day", 
      "values": [
        {
          "value": 13059, 
          "end_time": "2013-02-22T08:00:00+0000"
        }, 
        {
          "value": 15223, 
          "end_time": "2013-02-23T08:00:00+0000"
        },

And I using PHP to get the values of value and end_time the following foreach:

foreach ($page_impressions_organic['data'] as $page_impression_organic) {
    foreach($page_impression_organic['values'] as $key => $page_impression_organic_values) {
        $end_time = strval($page_impression_organic_values['end_time']);
        $ds = substr($end_time, 0, -14);
        $value = intval($page_impression_organic_values['value']);
        echo '[\'' . $ds . '\', ' . $value . ']';
}}

Everything is working like it should, my issue is that now I have a different json:

{
  "data": [
    {
      "id": "116376655202954/insights/page_storytellers_by_story_type/day", 
      "name": "page_storytellers_by_story_type", 
      "period": "day", 
      "values": [
        {
          "value": {
            "fan": 2043, 
            "page post": 293, 
            "user post": 5
          }, 
          "end_time": "2013-02-22T08:00:00+0000"
        }, 
        {
          "value": {
            "fan": 1085, 
            "page post": 398, 
            "user post": 5
          }, 
          "end_time": "2013-02-23T08:00:00+0000"
        }, 

and I am not able to get the same results because value is an array and I specially need only the page post values from it. I need to keep the form that my echo has for further scopes. I've made another foreach for like this:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {
        foreach($page_talking_about_values['value'] as $page_talking_about_value) {
            $end_time = strval($page_talking_about_values['end_time']);
            $ds = substr($end_time, 0, -14);
            $value = intval($page_talking_about_value['page post']);
            echo '[\'' . $ds . '\', ' . $value . ']';
}}}

but $value is 0 everytime... What I have to do?

3
  • Why not just decode your json string & work on it ? Commented Apr 22, 2013 at 13:03
  • end_time isn't within value. Get rid of the 3rd foreach loop. Commented Apr 22, 2013 at 13:03
  • PHP json_decode($jsonData, true); will return an multidimensional array Commented Apr 22, 2013 at 13:04

3 Answers 3

2

Its dead simple. See the rewritten code:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {
        $end_time = strval($page_talking_about_values['end_time']);
        $ds = substr($end_time, 0, -14);
        $value = intval($page_talking_about_values['value']['page post']);
        echo '[\'' . $ds . '\', ' . $value . ']';
    }
}

Your really do not have to iterate through the inner values array if you only need a known index. What happened in your solution is, that for each value in the nested value array you tried to get the index page post, but as the values of the nested array are not arrays the index page post returned a null which translatesd to 0 if fed into intval.

Beside that I really suggest to rename your variables to something more readable. Those long names with subtle differences make it difficult to read the code.

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

1 Comment

Thank you, it works like a charm. The variables names are for testing purposes for now, I will use Ajax to make a GET request to a php file anytime this kind of operation is needed.
1

I think you don't need the third for-loop because the value array only exists once per end-time.

Try to change your code like this:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {

        // note: removed third for-loop

        $end_time = strval($page_talking_about_values['end_time']);
        $ds = substr($end_time, 0, -14);

        // note: changed this line to directly read out the page posts
        $value = intval($page_talking_about_values['value']['page post']);

        echo '[\'' . $ds . '\', ' . $value . ']';
}}

Comments

0

You shud not directly loop through it. Decode the string using json_decode and then do foreach loop and then again json_encode it

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.