首页 > 解决方案 > PHP 警告:非法字符串偏移,错误

问题描述

在第 29 行出现错误,其中指出 $tree[$child['parent_id']]['children'][] = $child; 我正在使用 php5.5 错误日志显示:PHP 警告:第 29 行 /home/mysite.com/public_html/app/controllers/webController.php 中的非法字符串偏移 'parent_id'

     public function getchild($parentID) {
      $sql = "select id, parent_id from category where ";
      if ( is_null($parentID) ) {
          $sql .= "parent_id = 0";
      }
      else if ( is_array($parentID) ) {
          $parentID = implode(',', $parentID);
          $sql .= "parent_id in ({$parentID})";
      }
      else {
          $sql .= "parent_id = {$parentID}";
      }

      $tree = array();
      $idList = array();
      if($this->db->count($sql)){
          $res = $this->db->fetchAll($sql) ;
          if(count($res)>0){
            foreach($res as $row){
              $tree[$row['id']] = $row;
              $idList[] = $row['id'];
            }

          }

           if (isset($idList) && is_array($idList)) {
              $children = $this->getchild($idList);
              foreach($children as $child) {
                $tree[$child['parent_id']]['children'][] = $child;
              }
          }
          return $tree;
      }
      else
      {
        $tree['id'] = $parentID;
         return $tree;
      }
  }

请让我知道我做错了什么或如何解决错误。提前致谢

标签: php

解决方案


推荐阅读