1

I am working on an application where i am generating JSON using PHP. My code is here.

$arrayItem = array_slice($data, $start, $max);
$jsonArray = array();
$jsonArray['total'] = count($arrayItem);
for ($i = 0; $i <= 9; $i ++) {



     // Listomatic requires the "total" field to show/hide the "Show More" button

    // The following is sample data (in this case Powerball numbers) that you want to display

        $item = array(
            'id' => $arrayItem[$i]['id'],
            'title' => $arrayItem[$i]['title'],
            'featured_image' => $arrayItem[$i]['featured_image'],
            'audio_file' => $arrayItem[$i]['audio_file'],
            'youtube_id' => $arrayItem[$i]['youtube_id'],
            'open_par' => $arrayItem[$i]['open_par'],
            'category' => $arrayItem[$i]['category']);

        $list_array[] =  array('item' =>$item);

}

$jsonArray['items'] = $list_array;


header('Content-type: application/json');

echo json_encode($jsonArray);

It produces the following JSON. I am not sure why the zeros are being added.

{
    "total": 10,
    "items": [
        {
            "item": {
                "id": "234",
                "title": {
                    "0": "Indulged in any Good Vices Lately?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Social Issues"
                }
            }
        },
        {
            "item": {
                "id": "233",
                "title": {
                    "0": "Do You Use the Spirit of a Sound Mind?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Parable"
                }
            }
        },
        {
            "item": {
                "id": "232",
                "title": {
                    "0": "What Does it Mean to be Thankful?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Inspiration"
                }
            }
        },
        {
            "item": {
                "id": "226",
                "title": {
                    "0": "Who is Jesus?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        },
        {
            "item": {
                "id": "216",
                "title": {
                    "0": "Does God Really Torture People in Hellfire?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        },
        {
            "item": {
                "id": "192",
                "title": {
                    "0": "Why Don’t You Ever Listen to Me?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Character"
                }
            }
        },
        {
            "item": {
                "id": "188",
                "title": {
                    "0": "Who Ends Up in the “Lake of Fire?”"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        },
        {
            "item": {
                "id": "156",
                "title": {
                    "0": "Is Jesus Really God? (Parts III & IV)"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        },
        {
            "item": {
                "id": "155",
                "title": {
                    "0": "Is Jesus Really God? (Parts I & II)"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        },
        {
            "item": {
                "id": "124",
                "title": {
                    "0": "Did God Really Create the World in 6 Days?"
                },
                "featured_image": {
                    "0": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
                },
                "audio_file": {
                    "0": "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
                },
                "youtube_id": {},
                "open_par": {},
                "category": {
                    "0": "Doctrine"
                }
            }
        }
    ]
}

While I need the following JSON to be output.

{
    "total": 10,
    "items": [
        {
            "item": {
                "id": "234",
                "title":"Indulged in any Good Vices Lately?",
                "featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",

                "audio_file": "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3",

                "youtube_id": "http://youtube.com/",
                "open_par": "",
                "category": "Social Issues"

            }
        },
        {
            "item": {
                "id": "233",
                "title":"Do You Use the Spirit of a Sound Mind?",

                "featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",

                "audio_file": "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3",

                "youtube_id": "http://youtube.com/",
                "open_par": "",
                "category": "Parable"

            }
        },
        {
            "item": {
                "id": "232",
                "title": "What Does it Mean to be Thankful?",

                "featured_image": "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg",

                "audio_file": "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3",

                "youtube_id": "http://youtube.com/v",
                "open_par": "",
                "category": "Inspiration"

            }
        }
    ]
}

Forget about the blank values I just need the JSON structure. Please suggest whats wrong in my code. Thanks

Edit. My arrayItem var_dump is following.

array(10) {
  [0]=>
  array(7) {
    ["id"]=>
    string(3) "234"
    ["title"]=>
    object(SimpleXMLElement)#3306 (1) {
      [0]=>
      string(34) "Indulged in any Good Vices Lately?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3319 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3314 (1) {
      [0]=>
      string(94) "http://www.christianquestions.net/en/2002mp3/234_Indulge_in_any_good_vices_lately_12_15_02.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3315 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3310 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3323 (1) {
      [0]=>
      string(13) "Social Issues"
    }
  }
  [1]=>
  array(7) {
    ["id"]=>
    string(3) "233"
    ["title"]=>
    object(SimpleXMLElement)#3312 (1) {
      [0]=>
      string(38) "Do You Use the Spirit of a Sound Mind?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3325 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3320 (1) {
      [0]=>
      string(99) "http://www.christianquestions.net/en/2002mp3/233_Do_you_use_the_spirit_of_a_sound_mind_12_08_02.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3321 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3316 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3329 (1) {
      [0]=>
      string(7) "Parable"
    }
  }
  [2]=>
  array(7) {
    ["id"]=>
    string(3) "232"
    ["title"]=>
    object(SimpleXMLElement)#3318 (1) {
      [0]=>
      string(33) "What Does it Mean to be Thankful?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3331 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3326 (1) {
      [0]=>
      string(94) "http://www.christianquestions.net/en/2002mp3/232_What_does_it_mean_to_be_thankful_12_01_02.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3327 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3322 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3335 (1) {
      [0]=>
      string(11) "Inspiration"
    }
  }
  [3]=>
  array(7) {
    ["id"]=>
    string(3) "226"
    ["title"]=>
    object(SimpleXMLElement)#3324 (1) {
      [0]=>
      string(13) "Who is Jesus?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3337 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3332 (1) {
      [0]=>
      string(33) "http://www.christianquestions.net"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3333 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3328 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3341 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
  [4]=>
  array(7) {
    ["id"]=>
    string(3) "216"
    ["title"]=>
    object(SimpleXMLElement)#3330 (1) {
      [0]=>
      string(43) "Does God Really Torture People in Hellfire?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3343 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3338 (1) {
      [0]=>
      string(85) "http://www.christianquestions.net/en/2002mp3/216_Does_God_really_torture_Hellfire.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3339 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3334 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3347 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
  [5]=>
  array(7) {
    ["id"]=>
    string(3) "192"
    ["title"]=>
    object(SimpleXMLElement)#3336 (1) {
      [0]=>
      string(34) "Why Don’t You Ever Listen to Me?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3349 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3344 (1) {
      [0]=>
      string(92) "http://www.christianquestions.net/en/2002mp3/192_Why_dont_you_ever_listen_to_me_02_10_02.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3345 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3340 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3353 (1) {
      [0]=>
      string(9) "Character"
    }
  }
  [6]=>
  array(7) {
    ["id"]=>
    string(3) "188"
    ["title"]=>
    object(SimpleXMLElement)#3342 (1) {
      [0]=>
      string(38) "Who Ends Up in the “Lake of Fire?”"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3355 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3350 (1) {
      [0]=>
      string(84) "http://www.christianquestions.net/en/2002mp3/188_Who_ends_up_in_the_lake_of_fire.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3351 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3346 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3359 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
  [7]=>
  array(7) {
    ["id"]=>
    string(3) "156"
    ["title"]=>
    object(SimpleXMLElement)#3348 (1) {
      [0]=>
      string(37) "Is Jesus Really God? (Parts III & IV)"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3361 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3356 (1) {
      [0]=>
      string(72) "http://www.christianquestions.net/en/2001mp3/156_Is_Jesus_really_God.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3357 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3352 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3365 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
  [8]=>
  array(7) {
    ["id"]=>
    string(3) "155"
    ["title"]=>
    object(SimpleXMLElement)#3354 (1) {
      [0]=>
      string(35) "Is Jesus Really God? (Parts I & II)"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3367 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3362 (1) {
      [0]=>
      string(72) "http://www.christianquestions.net/en/2001mp3/155_Is_Jesus_really_God.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3363 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3358 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3371 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
  [9]=>
  array(7) {
    ["id"]=>
    string(3) "124"
    ["title"]=>
    object(SimpleXMLElement)#3360 (1) {
      [0]=>
      string(42) "Did God Really Create the World in 6 Days?"
    }
    ["featured_image"]=>
    object(SimpleXMLElement)#3373 (1) {
      [0]=>
      string(86) "http://www.christianquestions.net/wp-content/uploads/2004/05/cq_small_logo-310x227.jpg"
    }
    ["audio_file"]=>
    object(SimpleXMLElement)#3368 (1) {
      [0]=>
      string(96) "http://www.christianquestions.net/en/2000mp3/124_Did_God_create_the_world_in_6_days_10_22_00.mp3"
    }
    ["youtube_id"]=>
    object(SimpleXMLElement)#3369 (0) {
    }
    ["open_par"]=>
    object(SimpleXMLElement)#3364 (0) {
    }
    ["category"]=>
    object(SimpleXMLElement)#3377 (1) {
      [0]=>
      string(8) "Doctrine"
    }
  }
}
6
  • 1
    Check $arrayItem[$i]['title'] (and all the other elements), it's most probably an array itself. Commented Dec 7, 2013 at 22:49
  • 1
    Can you add a var_dump of $arrayItem. Most likely your data is different than what you expect. Commented Dec 7, 2013 at 22:49
  • 1
    Seems like $arrayItem has 3 dimensions instead of 2 as you expect. Commented Dec 7, 2013 at 22:49
  • 'title'=>$arrayItem[$i]['title'][0] and so on Commented Dec 7, 2013 at 22:53
  • I have added var_dump in my question edit. I will try your tip @apoq. Commented Dec 7, 2013 at 22:59

1 Answer 1

1

As seen in your var_dump, the title is not a string, but an object, causing this behavior. If you want json_encode to encode the value as a string, not as an object, make sure to give it a string, e.g. by casting the value:

        'title' => (string)($arrayItem[$i]['title']),

Edit: Note that String conversion is not always that simple. Just casting works in this case because we're dealing with SimpleXMLElement objects. If you have regular objects/arrays, you may need to use something more appropriate (e.g. $myarray[0]).

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

4 Comments

in that case 'title' will be a "Array" :)
and we'll receive "Array to string conversion" notice
@VaheShadunts: I just tested it with SimpleXMLElement objects (which is what OP is dealing with), and in that specific case, it seems to work. Edit: Since the answer got accepted, I'd assume OP tried it and it worked. I will specify that this does not work in all cases though.
Thanks Vahe Shadunts. It did the trick. I am getting correct JSON now. Thanks again for taking a look.

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.