Json parsing errors [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've got the following JSON and PHP and i'm i'm getting errors and I can't see what i've done wrong
Error
Notice: Trying to get property of non-object in C:\Users\tom.AD\PhpstormProjects\SYS\index.php on line 7
Notice: Trying to get property of non-object in C:\Users\tom.AD\PhpstormProjects\SYS\index.php on line 9
Warning: Invalid argument supplied for foreach() in C:\Users\tom.AD\PhpstormProjects\SYS\index.php on line 11
JSON
{
"apr": [
{
"f1": "139eg1",
"zones": [
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "unknown",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "top",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
}
]
},
{
"f1": "139eg2",
"zones": [
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "unknown",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "top",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
}
]
},
{
"f1": "139eg3",
"zones": [
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "unknown",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
},
{
"f2": "top",
"zone": "321"
},
{
"f2": "unknown",
"zone": "123"
}
]
}
]
}
PHP
$json_file = file_get_contents('json/posts.json');
$jfo = json_decode($json_file);
$title = $jfo->apr->f1;
$posts = $jfo->apr->zones;
foreach ($posts as $post) {
echo $post->zone;
}

apr is an array of objects, thus cannot be accessed with ->, according to your JSON you have supplied us. It seems you want to access all the zones. To do that, you would need to loop through the array with something like:
foreach ($jfo->apr as $object) {
print_r($object->zones);
}
To access them directly (since you have no keys and it is an indexed array), you'd need to do $jfo->apr[$key] where key is some numeric value that is an existing index in the array $jfo->apr. For example:
$first_zone_array = $jfo->apr[0]; //This gets the following:
object(stdClass)#2 (2) { ["f1"]=> string(6) "139eg1" ["zones"]=> array(5) { [0]=> object(stdClass)#3 (2) { ["f2"]=> string(7) "unknown" ["zone"]=> string(3) "123" } [1]=> object(stdClass)#4 (2) { ["f2"]=> string(7) "unknown" ["zone"]=> string(3) "321" } [2]=> object(stdClass)#5 (2) { ["f2"]=> string(7) "unknown" ["zone"]=> string(3) "123" } [3]=> object(stdClass)#6 (2) { ["f2"]=> string(3) "top" ["zone"]=> string(3) "321" } [4]=> object(stdClass)#7 (2) { ["f2"]=> string(7) "unknown" ["zone"]=> string(3) "123" } } }

Related

Issue in php json formatting

I am currently stuck in json formatting for php. I have given my outputted json below. What I need to do is to make the format of the current json to the desired one. I am missing the arrays in the JSON format. Can anyone help me on this.
My code to print the json output is below:
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
$final['MenuList']=$menuHead;
echo json_encode($final);
Current format:
{
"MenuList": {
"1": {
"menuHead": "Main Course",
"data": {
"1": {
"itemName": "Chicken Thai Curry",
"price": "599",
"description": "",
"itemId": "67",
"customizable": "1"
}
}
},
"2": {
"menuHead": "Refreshments",
"data": {
"2": {
"itemName": "Kingfisher Premium",
"price": "999",
"description": "Kingfisher beer",
"itemId": "69",
"customizable": "1"
},
"3": {
"itemName": "Mocktail",
"price": "999",
"description": "",
"itemId": "68",
"customizable": "1"
}
}
},
"4": {
"menuHead": "Rice biriyani",
"data": {
"4": {
"itemName": "Dal makni risotto",
"price": "499",
"description": "Dal makhni risotto",
"itemId": "66",
"customizable": "1"
}
}
}
}
}
Desired Format:
{
"menuList": [
{
"menuHead": "In Steamer (Momos)",
"data": [
{
"itemName": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
},
{
"itemName": "Rice and Noodles",
"data": [
{
"sub_category": "Tandoori Momo",
"description": "",
"price": "150",
"itemId": "16",
"customizable": "0"
},
{
"itemName": "Fried Momo Pork",
"price": "100",
"description": "",
"itemId": "15",
"customizable": "0"
}
]
}
]
}
Raw response is below:
array(4) { [0]=> array(7) { ["id"]=> string(2) "67" ["restaurant_id"]=> string(1) "5" ["category"]=> string(11) "Main Course" ["sub_category"]=> string(18) "Chicken Thai Curry" ["price"]=> string(3) "599" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [1]=> array(7) { ["id"]=> string(2) "69" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(18) "Kingfisher Premium" ["price"]=> string(3) "999" ["description"]=> string(15) "Kingfisher beer" ["customizable"]=> string(1) "1" } [2]=> array(7) { ["id"]=> string(2) "68" ["restaurant_id"]=> string(1) "5" ["category"]=> string(12) "Refreshments" ["sub_category"]=> string(8) "Mocktail" ["price"]=> string(3) "999" ["description"]=> string(0) "" ["customizable"]=> string(1) "1" } [3]=> array(7) { ["id"]=> string(2) "66" ["restaurant_id"]=> string(1) "5" ["category"]=> string(13) "Rice biriyani" ["sub_category"]=> string(17) "Dal makni risotto" ["price"]=> string(3) "499" ["description"]=> string(18) "Dal makhni risotto" ["customizable"]=> string(1) "1" } }
If you want a javascript compatible array, the index must start at 0. The easiest way to do that, is to use array_values():
$final['MenuList'] = array_values($menuHead);
The problem is that when your adding the data items, you need to add them without specific keys, as you add them with $i as in...
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
This will stop them being a normal array as you want it to be. For json_encode() an array must start at 0 and be sequential for it to be an array.
Instead create them in one go and add them to the end of the existing data using []...
$menuHead[$key]['data'][] = ['itemName' =>$value['sub_category'],
'price'=> $value['price'],
'description'=>$value['description'],
'itemId'=>$value['id'],
'customizable'=>$value['customizable']];
This needs to be done with each set of similar code, which includes the overall array itself, this can be done using
$final['MenuList'] = array_values($menuHead);
To try and fix the data you already have, which means no changes except adding the following code...
foreach ( $menuHead as $menu ) {
$menu['data'] = array_values($menu['data']);
}
$final['MenuList'] = array_values($menuHead);
Use array_values();
I fixed your code, it should work
$menuHead=array();
$i=0;
foreach($res as $key => $value){
$i=$key+1;
//$menuHead[$i]['menuHead']=$value['category'];
if(isset($menuHead[$key]['menuHead'])){
if($menuHead[$key]['menuHead']==$value['category']){
$menuHead[$key]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$key]['data'][$i]['price']=$value['price'];
$menuHead[$key]['data'][$i]['description']=$value['description'];
$menuHead[$key]['data'][$i]['itemId']=$value['id'];
$menuHead[$key]['data'][$i]['customizable']=$value['customizable'];
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}else{
$menuHead[$i]['menuHead']=$value['category'];
$menuHead[$i]['data'][$i]['itemName']=$value['sub_category'];
$menuHead[$i]['data'][$i]['price']=$value['price'];
$menuHead[$i]['data'][$i]['description']=$value['description'];
$menuHead[$i]['data'][$i]['itemId']=$value['id'];
$menuHead[$i]['data'][$i]['customizable']=$value['customizable'];
}
}
// i'am use array_values()
$final['MenuList']= array_values($menuHead);
echo json_encode($final);

Iterate through deeply nested array in PHP

I have data saved in JSON format (Prestashop) - I need to get to that data - which is deeply nested in arrays.
Here is the function:
public static function getAllCustomizedDatas($id_cart, $id_lang = null, $only_in_cart = true, $id_shop = null)
{
$datas = parent::getAllCustomizedDatas($id_cart, $id_lang, $only_in_cart, $id_shop);
var_dump($datas);
/*
* Iterate over $datas, you're looking for
* [id_product][id_product_attribute][id_address_delivery][id_customization][datas]
* Datas will contain an array of fields broken by their type. You can then decode
* the ones that need to be decoded and return the result:
*/
return $datas;
}
if I var_dump $datas I see this (I formatted this to make it a little easier for myself to read):
array(1) {
[8]=> array(1) {
[0]=> array(1) {
[0]=> array(2) {
[22]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "22"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
[23]=> array(4) {
["datas"]=> array(1) {
[1]=> array(1) {
[0]=> array(9) {
["id_customization"]=> string(2) "23"
["id_address_delivery"]=> string(1) "0"
["id_product"]=> string(1) "8"
["id_customization_field"]=> string(1) "2"
["id_product_attribute"]=> string(1) "0"
["type"]=> string(1) "1"
["index"]=> string(1) "2"
["value"]=> string(615) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
["name"]=> string(20) "Client Customization"
}
}
}
["quantity"]=> int(2)
["quantity_refunded"]=> int(0)
["quantity_returned"]=> int(0)
}
}
}
}
}
What will be the easiest way to get to the ["value"] portion of the deeply nested array?
This has to be fairly dynamic because depending on the amount of items this user has - the amount of arrays will change. In this example, there are 2 there (each with 2 items in 'value'). A user could add 3 or 4 or 10 items if they wanted to. But I'm just trying to get to ['value'] and convert that JSON into HTML for the view this is getting passed to.
Bonus: Know of a way to easily iterate through the JSON data?
The JSON data looks like this:
[
[{
"name": "item[1][line1]",
"customization": "asdf"
}, {
"name": "item[1][line2]",
"customization": ""
}, {
"name": "item[1][line3]",
"customization": ""
}, {
"name": "item[1][line4]",
"customization": ""
}, {
"name": "item[1][line5]",
"customization": ""
}, {
"name": "item[1][line6]",
"customization": ""
}, {
"name": "item[1][line7]",
"customization": ""
}],
[{
"name": "item[2][line1]",
"customization": "asdf"
}, {
"name": "item[2][line2]",
"customization": ""
}, {
"name": "item[2][line3]",
"customization": ""
}, {
"name": "item[2][line4]",
"customization": ""
}, {
"name": "item[2][line5]",
"customization": ""
}, {
"name": "item[2][line6]",
"customization": ""
}, {
"name": "item[2][line7]",
"customization": ""
}]
]
I ended up just nesting a bunch of foreach statements until I had arrived at the array I wanted to. It was ugly, but functional.
Then I did json_decode across the JSON and iterated across it.
https://packagist.org/packages/ishworkh/multi-level-array-iterator
This might be helpful in iterating through deep nested array. And then use key or hierarchy information to filter out value needed for you.
Lets say this is your array
$array = [
[
[
[
[
"datas" => [
[
[
"id_customization" => "22",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
[
[
[
[
"id_customization" => "23",
"id_address_delivery" => "0",
"id_product" => "8",
"id_customization_field" => "2",
"id_product_attribute" => "0",
"type" => "1",
"index" => "2",
"value" => '[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]',
"name" => "Client Customization",
],
],
],
"quantity" => 2,
"quantity_refunded" => 0,
"quantity_returned" => 0,
],
],
],
],
];
You could create a function like this, that uses multi-level-array-iterator's iterate method to go over all the nested array and
yield out found values.
/**
* #param array $nestedArray
*
* #return Generator
*/
function extractValuesFromArray(array $nestedArray):Generator
{
// $key is the local index key, which in this case should be 'value' we are looking to filter for
foreach(\ArrayIterator\ArrayIteratorFacade::iterate($nestedArray) as $key => $ArrayElement)
{
if ('value' === $key)
{
yield $ArrayElement->getValue();
}
}
}
var_dump(iterator_to_array(extractValuesFromArray($array)));
should give
array(2) {
[0]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
[1]=>
string(643) "[[{"name":"item[1][line1]","customization":"asdf"},{"name":"item[1][line2]","customization":""},{"name":"item[1][line3]","customization":""},{"name":"item[1][line4]","customization":""},{"name":"item[1][line5]","customization":""},{"name":"item[1][line6]","customization":""},{"name":"item[1][line7]","customization":""}],[{"name":"item[2][line1]","customization":"asdf"},{"name":"item[2][line2]","customization":""},{"name":"item[2][line3]","customization":""},{"name":"item[2][line4]","customization":""},{"name":"item[2][line5]","customization":""},{"name":"item[2][line6]","customization":""},{"name":"item[2][line7]","customization":""}]]"
}

remove duplicate values from multidimensional array

Below is the result and I want to remove duplicate from the array
I tried using this code: $login_data1['items'] = array_values(array_map("unserialize", array_unique(array_map("serialize", $login_data1['items']))));
{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual",
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
],
}
I tried using $login_data1['items'] = array_unique($login_data1['items'] ,SORT_REGULAR); but this adds serial numbers at the each json response
Try as using array_unique
$json = '{
"items": [
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
},
{
"id": "1",
"tags": [
{
"name": "Snow Leopard"
}
],
"type": "faq"
},
{
"id": "2",
"tags": [
{
"name": "Microsoft"
}
],
"type": "manual"
}
]
}';
foreach(json_decode($json, true) as $key => $value){
$input = array_unique($value,SORT_REGULAR);
}
If its an array then simply use
array_unique($login_data['items'],SORT_REGULAR);
Fiddle
array_unique works perfectly if you pass a multidimensional array.
$login_data1['items'] = array_unique($login_data1['items'], SORT_REGULAR);
It doesn't work with your json because it's an array of object. Infact:
$array = json_decode($json);
var_dump($array);
returns:
object(stdClass)#1 (1) {
["items"]=>
array(3) {
[0]=>
object(stdClass)#2 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#3 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
[1]=>
object(stdClass)#4 (3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#5 (1) {
["name"]=>
string(12) "Snow Leopard"
}
}
["type"]=>
string(3) "faq"
}
[2]=>
object(stdClass)#6 (3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
[0]=>
object(stdClass)#7 (1) {
["name"]=>
string(9) "Microsoft"
}
}
["type"]=>
string(6) "manual"
}
}
}
Your json should look like this:
{
"items": [
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "1",
"tags": {
"name": "Snow Leopard"
},
"type": "faq"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
},
{
"id": "2",
"tags": {
"name": "Microsoft"
},
"type": "manual"
}
]
}
And now:
$array = json_decode($json);
var_dump($array);
returns:
array(1) {
["items"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[1]=>
array(3) {
["id"]=>
string(1) "1"
["tags"]=>
array(1) {
["name"]=>
string(12) "Snow Leopard"
}
["type"]=>
string(3) "faq"
}
[2]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
[3]=>
array(3) {
["id"]=>
string(1) "2"
["tags"]=>
array(1) {
["name"]=>
string(9) "Microsoft"
}
["type"]=>
string(6) "manual"
}
}
}
And array_unique works.
I got the solution for this.
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));
Try array_value and array_unique together and serial number will be removed!
$login_data1['items'] =array_values(array_unique($login_data1['items'] ,SORT_REGULAR));

Parsing JSON gives few separate elements put toghether as one

I get following JSON (which I have validated here):
{
"name": "Brian",
"agentid": "12345",
"username": "bob",
"passcode": "bob01",
"call": {
"aa": "11",
"bb": "22",
"cc": "33",
"dd": "44"
},
"call": {
"aa": "111",
"bb": "222",
"cc": "333",
"dd": "444"
},
"call": {
"aa": "1111",
"bb": "2222",
"cc": "3333",
"dd": "4444"
},
"call": {
"aa": "11111",
"bb": "22222",
"cc": "33333",
"dd": "44444"
}
}
When I put this through var_dump(json_decode($json, true));, it gives me:
array(5) {
["name"]=> string(5) "Brian"
["agentid"]=> string(5) "12345"
["username"]=> string(3) "bob"
["passcode"]=> string(5) "bob01"
["call"]=> array(4) {
["aa"]=> string(5) "11111"
["bb"]=> string(5) "22222"
["cc"]=> string(5) "33333"
["dd"]=> string(5) "44444"
}
}
It looks like the first three call elements are overwritten.
How to parse this JSON and retain all call elements?
This is valid JSON syntax, but content makes no sense. Having more the one key of the same name is wrong. You should make single call and turn it into JSON array where you would store all objects:
{
"name":"Brian",
"agentid":"12345",
"username":"bob",
"passcode":"bob01",
"call":[
{
"aa":"11",
"bb":"22",
"cc":"33",
"dd":"44"
},
{
"aa":"111",
"bb":"222",
"cc":"333",
"dd":"444"
},
{
"aa":"1111",
"bb":"2222",
"cc":"3333",
"dd":"4444"
},
{
"aa":"11111",
"bb":"22222",
"cc":"33333",
"dd":"44444"
}
]
}

PHP json_encode produces wrong JSON results. Why?

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"
}
}
}
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]).

Categories