Search code examples
phparray-push

initialization for array of arrays in PHP


I want to dynamically create arrays of arrays and I have no clue how to initialize my arrays...

Here is the code:

$resT= array();
$resR= array();
foreach ($cursor as $obj) {
   if ($obj == NULL) continue;
   $c="XX";
   if (test1)         $c=$obj['GC'];
   array_push($resT[$c],$obj['AT']);
   array_push($resR[$c],$obj['AR']);
}

I got this: array_push() expects parameter 1 to be array

Thanks,

Amir.


Solution

  • $resT= array();
    $resR= array();
    foreach ($cursor as $obj) {
       if ($obj == NULL) continue;
       $c="XX";
       if (test1)         $c=$obj['GC'];
        if(!isset($resT[$c]))
          $resT[$c] = array();
        if(!isset($resR[$c]))
          $resR[$c] = array();
       array_push($resT[$c],$obj['AT']);
       array_push($resR[$c],$obj['AR']);
    }