2013-12-07 2 views
1

Я работаю над приложением, в котором я создаю JSON, используя PHP. Мой код здесь.PHP json_encode производит неправильные результаты JSON. Зачем?

$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); 

Он производит следующие JSON. Я не уверен, почему добавляются нули.

{ 
    "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" 
       } 
      } 
     } 
    ] 
} 

Хотя мне нужен следующий JSON для вывода.

{ 
    "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" 

      } 
     } 
    ] 
} 

Забудьте о пустых значениях Мне просто нужна структура JSON. Пожалуйста, предложите, что не так в моем коде. Thanks

Редактировать. Мой arrayItem var_dump следующий.

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" 
    } 
    } 
} 
+1

Проверить '$ arrayItem [$ i] ['title']' (и все остальные элементы), это, скорее всего, сам массив. – raina77ow

+1

Можете ли вы добавить var_dump '$ arrayItem'. Скорее всего, ваши данные отличаются от того, что вы ожидаете. –

+1

Кажется, что $ arrayItem имеет 3 измерения вместо 2, как вы ожидаете. –

ответ

1

Как видно из вашего var_dump, заголовок не является строкой, а объектом, вызывающим это поведение. Если вы хотите, чтобы json_encode кодировал значение в виде строки, а не как объект, не забудьте указать ему строку, например. путем литья значения:

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

Редактировать: Обратите внимание, что преобразование строк не всегда так просто. Просто кастинг работает в этом случае, потому что мы имеем дело с объектами SimpleXMLElement. Если у вас есть обычные объекты/массивы, вам может потребоваться использовать что-то более подходящее (например, $myarray[0]).

+1

В этом случае «title» будет «Array» :) –

+0

, и мы получим уведомление «Array to string conversion» –

+0

@VaheShadunts: Я просто протестировал его с объектами SimpleXMLElement (с которым работает OP), и в этом конкретном случае это работает. Изменить. Поскольку ответ был принят, я бы предположил, что ОП попробовал его, и это сработало. Я укажу, что это не работает во всех случаях. –

Смежные вопросы