I’m working on magento site and facing strange error when array values assign inside function and retrieve outside of function.
//define array
$ctall=array();
//function for array value assign
function printtest($fg){
//global variable
global $ctall;
//just assign to array
$ctall[rand(10000,100000)]=$fg;
//this var dump shows array with vaues when function calling
// var_dump($ctall);
}
i call the function here inside an another function
$categoryTree = Mage::getResourceModel('catalog/category')->getCategories($categoryId, 0, true);
$printCategories = function($nodes) use (&$printCategories) {
foreach ($nodes as $_category):
$ctdf=$_category->getId();
$categoryn = Mage::getModel('catalog/category')->load($ctdf);
if($ctdf!='' && $categoryn->getIsActive()):
//here call to function by passing a value
printtest($ctdf);
$printCategories($_category->getChildren());
endif;
endforeach;
};
$printCategories($categoryTree);
//sleep(10);
// i try to get array results here but it shows empty
var_dump($ctall);
Anyone know how to fix this, i tried hours without luck. Thank You
2
Answers
Try to push instead of assigning.Try this:
you can try this also:
remove all declaration of $ctall, and try this:
on outside, dump like this: