Php 从变量名创建数组

Php 从变量名创建数组,php,sql,arrays,Php,Sql,Arrays,我试图从一个变量名创建一个数组。我想从sql表中获取信息,并将该信息保存在数组中,但是我得到一个错误,错误是“不能使用[]进行读取”。为什么? 不要让树木遮住森林: $foo = []; // OK (create empty array with the PHP/5.4+ syntax) $foo[] = 10; // OK (append item to array) echo $foo[0]; // OK (read one item) echo $foo[]; // Error (wha

我试图从一个变量名创建一个数组。我想从sql表中获取信息,并将该信息保存在数组中,但是我得到一个错误,错误是“不能使用[]进行读取”。为什么?


不要让树木遮住森林:

$foo = []; // OK (create empty array with the PHP/5.4+ syntax)
$foo[] = 10; // OK (append item to array)
echo $foo[0]; // OK (read one item)
echo $foo[]; // Error (what could it possibly mean?)
表示法需要字符串(文字或变量):

数组(1){
[0]=>
内部(33)
}
尽管如此,您的方法看起来是生成无法维护代码的极好方法。为什么不使用具有已知名称的数组

$products[$current_product_name][] = $row;

不要让树木遮住森林:

$foo = []; // OK (create empty array with the PHP/5.4+ syntax)
$foo[] = 10; // OK (append item to array)
echo $foo[0]; // OK (read one item)
echo $foo[]; // Error (what could it possibly mean?)
表示法需要字符串(文字或变量):

数组(1){
[0]=>
内部(33)
}
尽管如此,您的方法看起来是生成无法维护代码的极好方法。为什么不使用具有已知名称的数组

$products[$current_product_name][] = $row;

非常感谢。首先是答案,然后是建议!非常感谢。首先是答案,然后是建议!