0

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

4
  • 1
    try this example 3v4l.org/SVfrM Commented Jan 25, 2018 at 11:14
  • 1
    I don't think all code what you shown are in the same file. because if it was it have to work. (As i can see second-code used amgento syntaxes while first-code is normal php function syntaxes) Commented Jan 25, 2018 at 11:17
  • above one also not working, the first function i use for explain simply which also have some magento codes too Commented Jan 25, 2018 at 11:18
  • you think its a php setting or something Commented Jan 25, 2018 at 11:24

2 Answers 2

2

remove all declaration of $ctall, and try this:

//remove define array, don't define it
// $ctall=array();

function printtest($fg){

    if(!isset($GLOBALS['ctall'])){
        $GLOBALS['ctall'] = array();
    }
    //assign to global
    $GLOBALS['ctall'][rand(10000,100000)]=$fg;
}

on outside, dump like this:

var_dump($GLOBALS['ctall'])
Sign up to request clarification or add additional context in comments.

7 Comments

same results mate array(0) { }
Maybe you call from inside another function or class, try add: global $ctall; before: var_dump($ctall);
no mate it's outside the functions, i just add that to test after add var_dump print null instead of array(0) { }
try to declare static $ctall=array(); on before $categoryTree = Mage::getResourceModel..... and delete declaration on before the function, make sure you just declare $ctall once.
okay, the last solution, you can use $_GLOBALS variable directly, I will edit my answer
|
1

Try to push instead of assigning.Try this:

$ctall[][rand(10000,100000)]=$fg; //notice the empty square brackets

you can try this also:

function printtest($fg){
  global $ctall;
  $new_array =array();
  $new_array[rand(10000,100000)] = $fg;
  array_merge($ctall, $new_array);
}

7 Comments

Are you serious right now? it will give him another issue also.->can't use empty index for assignmenrt
Now it even worse.You are unnecessarly using extra code.
@Alive to Die : Can you then tell us how to push element into associative array? I mean push not assign
In OP's case there is no need of push. His question is why global is also not working in his case. Your answer is far away from what he is asking
but did you realize that I'm trying to push and not make assignment?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.