php - Cakephp: Cannot use string offset as an array -



php - Cakephp: Cannot use string offset as an array -

error message

error: cannot utilize string offset array file: c:\wamp\www\itsm\app\model\taskattribute.php line: 48

lines of error 47 foreach($items $row){ 48 $categoryid = $row['category']['id']; 49 $item_id = $row['item']['id']; 50 $attr_id = $row['attribute']['id']; my method 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 $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 $key=>$items){ if(!ifexist($items ,'items')){ unset($tree[$key]); } } homecoming $tree; } }

find list returns list

this code in question:

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

returns flat list - i.e. $items of structure:

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

as such - attempting treat 'value string' array results in error.

foreach($items $item_id => $item_id) { // ^ it's string // won't work: // $categoryid = $item_id['category']['id']; $categoryid = ...; // not in results $item_id = ...; // it's key , value returned find list $attr_id = // not in results

as indicated above, 2 of 3 bits of info code tries obtain aren't in results @ all.

use find homecoming array

the simplest solution phone call find all:

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

only restrict fields returned after verifying required info present.

php arrays cakephp

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -