我很难根据从 mysql 数据库中获取的查询构建多维数组。目的是为 json 编码准备结果。这里有问题。
我的目标是:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
[69] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
...etc
)
从:
//this is a list of unknown length
$array_ids = (68, 69, 70, etc... ); // or:
Array (
[0] => 68
[1] => 69
[2] => 70
[3] => 71
etc..
)
//this is a known length
$array_contents = ( array ( array ( [id], [description]) ); // or:
Array (
[0] => Array (
[id] => 64
[description] =>yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada... )
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
我的尝试:
foreach($array_ids as $row){
$result = array($row=>array());
foreach($array_contents as $key => $value){
$result [$row][$key] = $value;
}
}
结果:
Array (
[68] => Array (
[0] => Array (
[id] => 64
[description] => yada, yada, yada... )
[1] => Array (
[id] => 65
[description] => yada, yada, yada...)
[2] => Array (
[id] => 66
[description] => yada, yada, yada... ) )
)
......这就是它停止的地方。它不会继续到 69、70 等的下一行 id...
我究竟做错了什么?