为json对象数组的foreach()错误提供的参数无效

时间:2017-09-27 06:40:28

标签: php arrays json

我正在将json对象数组解码为html列表。我尝试了一些它工作的演示但是当我处理这个json数组时,这给出了错误:为foreach()提供了无效的参数。我错过了什么?

    <?php
$json_string = '{"error":false,"data":[{"jb_product_category_id":"1","jb_product_category_name":"Mother","jb_product_category_prefix":"jbpm","jb_product_category_delete_status":"0","jb_product_category_created_on":"1501500876531","jb_product_category_updated_on":"1501500876531","subCategory1":[{"jb_product_subcategory1_1_id":"1","jb_product_subcategory1_2_category_id":"1","jb_product_subcategory1_3_name":"Cloths","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1501563015164","jb_product_subcategory1_6_updated_on":"1501563015164","subCategory2":[{"jb_product_subcategory2_1_id":"1","jb_product_subcategory2_2_category_id":"1","jb_product_subcategory2_3_subcategory1_id":"1","jb_product_subcategory2_4_name":"Pregnancy wear","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1501574226464","jb_product_subcategory2_7_updated_on":"1501574226464"}]}]},{"jb_product_category_id":"2","jb_product_category_name":"Child Wear","jb_product_category_prefix":"jbpc","jb_product_category_delete_status":"0","jb_product_category_created_on":"1502429483534","jb_product_category_updated_on":"1502429483534","subCategory1":[{"jb_product_subcategory1_1_id":"2","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Girls","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1502429606169","jb_product_subcategory1_6_updated_on":"1502429606169","subCategory2":[{"jb_product_subcategory2_1_id":"2","jb_product_subcategory2_2_category_id":"2","jb_product_subcategory2_3_subcategory1_id":"2","jb_product_subcategory2_4_name":"Western","jb_product_subcategory2_5_delete_status":"0","jb_product_subcategory2_6_created_on":"1502429794573","jb_product_subcategory2_7_updated_on":"1502429794573"}]},{"jb_product_subcategory1_1_id":"3","jb_product_subcategory1_2_category_id":"2","jb_product_subcategory1_3_name":"Boys","jb_product_subcategory1_4_delete_status":"0","jb_product_subcategory1_5_created_on":"1505105190176","jb_product_subcategory1_6_updated_on":"1505105190176","subCategory2":[]}]}]}';

    $array = json_decode($json_string, true);
    function build_list($array) {
    $list = '<ul>';
    foreach($array as $key => $value) {
    foreach($value as $key => $index) {
     if(is_array($index)) {
       $list .= build_list($index);
        } else {
       $list .= "<li>$index</li>";
      }
     }
    }

      $list .= '</ul>';
    return $list;
        }

echo build_list($array);
    ?>

2 个答案:

答案 0 :(得分:1)

只使用一个foreach,嵌套的似乎没用:

foreach($array as $key => $index) {
  if(is_array($index)) {
  /* ... */

答案 1 :(得分:0)

只需添加条件即可检查$value是否为有效数组。这样它就不会处理$value如果它不是数组并且警告会消失。

    if (!is_array($value)) {
        continue;
    }

在循环foreach之前在$array内使用此条件。

foreach($array as $key => $value) {
    if (!is_array($value)) {
        continue;
    }

    foreach($value as $k => $index) {
     if(is_array($index)) {
       $list .= build_list($index);
        } else {
       $list .= "<li>$index</li>";
      }
    }
}

Ideone链接:Code