为什么我不能为每个循环访问a之外的数组?

时间:2019-01-23 00:12:35

标签: php arrays global

我似乎无法传递给php for loop之外的变量。

我在做什么错了?

我正在尝试从查询中构建数组 例如

array() [ "A" =>array() [ key=>value, key=>value, key=>value], 
          "B" => array() [ key=>value, key=>value, key=>value]
          etc etc etc

查询返回预期结果

$sorted=Array();
foreach(range('A', 'Z') as $letter) {
    $rows=Array();
    $query="SELECT `Country`, ROUND(`PeakRate1`, 2) as Rate FROM `VoicePriceLists` where `Country` like '$letter%' and `PeakRate1` != 0 and `DiallingCode` != '64' and `DiallingCode` != 'restricted' group by `Country`";
    if ($result = $link->query($query)) {
        if( $result->num_rows == 0 ) {
            return FALSE;
        } else {
            while($row=$result->fetch_assoc()) {
                // var_dump($letter);
                $rows[]=$row;
            }
        }
        $result->close();
    } else {
        printf("Something major went wrong :( ");
    }
    $sorted[$letter]=$rows;
    var_dump($sorted); // I CAN SEE THE ARRAY GETTING LARGER HERE
}
var_dump($sorted); // NOTHING IN THE ARRAY

预期:

Array("A"=>array(key=>value, etc), "B"=>array(key=>value, etc))

0 个答案:

没有答案