dongxian3418 2017-09-27 06:40
浏览 210
已采纳

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

I am decoding the array of json objects into html lists . i have tried with some demo it worked but when i deal with this array of json this gives error as : Invalid argument supplied for foreach(). what is i am missing ?

    <?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条回答 默认 最新

  • duanhui9840 2017-09-27 06:50
    关注

    Simply use only one foreach, the nested one seems useless :

    foreach($array as $key => $index) {
      if(is_array($index)) {
      /* ... */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?