Return collection or multidimensional array or array inside array in php

1.5k views Asked by At

This question already asked but did not find any good answer.

To retrieve categories and subcategories in magento I am using this function:

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToSort('path', 'asc')
    ->addFieldToFilter('is_active', array('eq'=>'1'))
    ->load()
    ->toArray();

// Arrange categories in required array
$categoryList = array();
foreach ($categories as $catId => $category) {
    if (isset($category['name'])) {
        $categoryList[] = array(
            'label' => $category['name'],
            'level'  =>$category['level'],
            'value' => $catId
        );
    }
}

after that if I simply use return $categoryList; it will not return anything(just blank) output like:

[
    [
    ],
    [
    ],
    [
    ]
]

but if I use return array(array($categoryList)); in that case it will give right output.

Like this:(checking with json format)

[
    [
        [
            {
                "label": "hello",
                "level": "1",
                "value": 2
            },
            {
                "label": "hello1",
                "level": "2",
                "value": 10
            },
            {
                "label": "hello3",
                "level": "3",
                "value": 17
            },
            {
                "label": "Tunics",
                "level": "3",
                "value": 18
            }
       ]
   ]
]

but there are two more brackets and I do not want them and as I am new to php I do not have much Idea about how to remove them.

Is there any method which can make it right or I have to create array with different style.

2

There are 2 answers

7
GauravGD On

try this code

    $categoryList = array();
    $i = 0;
    foreach ($categories as $catId => $category) {
        if (isset($category['name'])) {
            $categoryList[$i]['label'] = $category['name'];
            $categoryList[$i]['level'] = $category['level'];
            $categoryList[$i]['value'] = $catId;
            $i++;
        }
    }
    return json_encode( $categoryList);

Output:

[{"label":"hello","value":2,"level":"0"},{"label":"hello","value":2,"level":"1"},{"label":"hello","value":2,"level":"2"}]

if this doesn't work for you try this

var_dump($categoryList) if it was like this

array(1) {
    [0]=>
      array(1) {
        [0]=>
        array(1) {
          [0]=>
          array(3) {
            ["label"]=>
            string(5) "hello"
            ["level"]=>
            string(1) "1"
            ["value"]=>
            int(2)
          }
        }
      }
    }

then

$response = json_encode($categoryList[0][0][0]);

Output:

{"label":"hello","level":"1","value":2}
0
Flow On

First of all you can use Mage::log($myvar, Zend_Log::ERR); to properly display your variable / array. The outpout is in your root directory / var/log/system.log

Then try to log $categoryList; and check the format of your response. If you have too much bracket in the result you can use this simple hack :

foreach ($categoryList as $category){
    Mage::log($category);
}

Hope this help Cheers