php - Cakephp:无法将字符串偏移量用作数组

标签 php arrays cakephp

错误消息

Error: Cannot use string offset as an array
File: C:\wamp\www\itsm\app\Model\TaskAttribute.php
Line: 48

错误行

47    foreach($items as $row){
48          $categoryId = $row['Category']['id'];
49          $item_id =  $row['Item']['id'];
50          $attr_id = $row['Attribute']['id'];

我的方法

public function getTree($branch_id = NULL){
        $tree = array();
        $items =  $this->find('list',array(
            'fields' => array('ItemAttribute.item_id','ItemAttribute.item_id'),
            'joins' =>array(
                        array(
                            'table' => 'item_attributes',
                            'alias' => 'ItemAttribute',
                            'type' => 'INNER',
                            'conditions' => 'TaskAttribute.item_attribute_id = ItemAttribute.id',
                         ),                 


                ),
                'recursive'=>-1
            )
        );  



        if(isset($items)){
            $assets = $this->Checklist->ChecklistAttribute->AssetAttribute->Asset->getList($branch_id ,array_keys($items));
            //pr($assets);die();
        }

        foreach($items as $row){
            $categoryId = $row['Category']['id'];
            $item_id =  $row['Item']['id'];
            $attr_id = $row['Attribute']['id'];

            if(!isset($tree[$categoryId])){
                $tree[$categoryId] = array('id'=>$categoryId ,'name'=>$row['Category']['name'] ,'Items'=>array());

            }


            $attrib = array('id'=>$row['Attribute']['id'] ,'name'=>$row['Attribute']['name'],'type'=>$row['Attribute']['type']);

            if(isset($items[$item_id])){

                if($asset = ifExist($assets ,$item_id)){
                    if(!isset($tree[$categoryId]['Items'][$item_id])){
                        $tree[$categoryId]['Items'][$item_id]  = array('id'=>$item_id ,'name'=>$row['Item']['model']);
                    }                       
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets'])){
                        $tree[$categoryId]['Items'][$item_id]['Assets'] = array('id'=>$asset['id'] ,'name'=>$asset['serial'] ,'Attributes'=>array());
                    }
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id])){
                        $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id] = $attrib;
                    }
                    if($option_id = ifExist($row ,'AttributeOptions' ,'id')){
                        if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'])){
                            $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'] = array();
                        }
                        $tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'][$option_id] = $row['AttributeOptions']['label'];
                    }
                }
            }else{

                if(!isset($tree[$categoryId]['Items'][$item_id])){
                    $tree[$categoryId]['Items'][$item_id]  = array('id'=>$item_id ,'name'=>$row['Item']['model'] ,'Attributes'=>array());
                }           
                if(!isset($tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id])){
                    $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id] = $attrib;
                }       
                if($option_id = ifExist($row ,'AttributeOptions' ,'id')){   
                    if(!isset($tree[$categoryId]['Items'][$item_id]['Assets']['Attributes'][$attr_id]['options'])){
                        $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id]['options'] = array();
                    }
                    $tree[$categoryId]['Items'][$item_id]['Attributes'][$attr_id]['options'][$option_id] = $row['AttributeOptions']['label'];
                }
            }


        }

            if(isset($tree)){
                foreach($tree as $key=>$items){
                    if(!ifExist($items ,'Items')){
                    unset($tree[$key]);
                }
            }

            return $tree;
        }

    }   

最佳答案

查找列表返回列表

问题中的这段代码:

$items =  $this->find('list',array(
    'fields' => array('key field','value field')
    ...
));

返回一个平面列表 - 即 $items 的结构为:

array(
    'key' => 'value string',
    'key2' => 'value string2',
);

因此 - 尝试将“值字符串”视为数组会导致错误。

foreach($items as $item_id => $item_id) {
    //                        ^ It's a string

    // So this won't work:
    // $categoryId = $item_id['Category']['id'];

    $categoryId = ...; // not in the results
    $item_id =  ...; // it's the key and the value returned by find list
    $attr_id = // not in the results

如上所述,代码尝试获取的三位信息中的 2 位根本不在结果中。

使用 find all 返回一个数组

最简单的解决方案就是调用 find all:

$items =  $this->find('all',array(
    //'fields' => array('key field','value field')
    ...
));

debug($items);
die;

仅限制在验证所有必需数据均存在后返回的字段。

关于php - Cakephp:无法将字符串偏移量用作数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340922/

相关文章:

php - CakePHP 中的 Feeds 错误返回 "Invalid xml declaration"

cakephp - mysql-real-escape-string : Access denied in CakePHP

php - API响应: Access member with hyphen in name

php - 使用 PHP 显示存储在 mySQL 数据库中的图像

php - 有没有办法测试 "isForEachable"的变量

c - 使用 malloc 从函数返回数组

php - 从 PHP、mysql(页面中的多个查询)导出到 excel,代码无法正常运行?

java - 将分数转换为带分数 - Java 方法

php - 我的 json 网络服务意外地产生了斜杠

PHP 修复错误文本