2 回答
TA贡献1865条经验 获得超7个赞
结果和预期结果基本上是一样的,因为正如@Nick 指出的那样,您需要拥有顶级键(0,1,2)。(否则你如何识别哪个 id 属于 john、paul 和 george?)
根本不可能存储这样的数组:
Array ( [id] => 1 [name] => john [age] => 44 )
Array ( [id] => 2 [name] => paul [age] => 30 )
Array ( [id] => 3 [name] => george [age] => 25 )
它必须像这样存储:(使用顶级键(在本例中为:0,1 和 2))
Array (
[0] => Array ( [id] => 1 [name] => john [age] => 44 )
[1] => Array ( [id] => 2 [name] => paul [age] => 30 )
[2] => Array ( [id] => 3 [name] => george [age] => 25 )
)
的价值$data[0]['name']将是约翰,价值$data[1]['name']将是保罗等。
如果您引用$data[0]它包含数组:Array ( [id] => 1 [name] => john [age] => 44 )
如果您引用$data[1]它包含数组:Array ( [id] => 2 [name] => paul [age] => 30 ) 等
不可能根据需要存储值,但当然可以根据需要输出:
//$data_value is an array in every row
foreach($data as $data_value) {
print_r($data_value);
}
这将产生输出:
Array
(
[id] => 1
[name] => john
[age] => 44
)
Array
(
[id] => 2
[name] => paul
[age] => 30
)
Array
(
[id] => 3
[name] => george
[age] => 25
)
TA贡献1883条经验 获得超3个赞
做这个:
$data = [];
$i = 0;
while ($row = mysqli_fetch_assoc($sql)) {
foreach( $row as $field => $value) {
$data[$i][$field] = $value:
}
$i++;
}
print_r($data)
- 2 回答
- 0 关注
- 83 浏览
添加回答
举报