我需要创建一个带有foreach循环和while循环的多维数组。
第一个数组包含:
Array
(
[0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19
)
我需要让它看起来像这样:
Array
(
[0] => Array
(
[date] => 13-10-14
[id] => Array
(
[0] => 012643
[1] => 012667
[2] => 013362
[3] => 016169
[4] => 016839
[5] => 035288
[6] => 035369
[7] => 037664
[8] => 038979
[9] => 039014
[10] => 039036
[11] => 039505
)
)
)
第一个数组我做了一个foreach循环,我需要做第二个,因为它是一个sql查询。
以下是代码:
foreach ($rs as $results) {
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$results['id'][] = $rsT;
}
$l_array[] = $results;
}## Heading ##
print_r($l_array);
返回错误:
Fatal error: Can not use string offset to an array
答案 0 :(得分:0)
如果你的$ rs是这个:
array([0] => 13-10-14
[1] => 13-10-15
[2] => 13-10-16
[3] => 13-10-17
[4] => 13-10-18
[5] => 13-10-19)
你的foreach()会将$ results设置为:13-10-15,13-10-16等等。我认为这是你的错误,因为那些是字符串,而不是数组。怎么样:
$final_array = array();
foreach ($rs as $results) {
$tmp_array = array('date'=>$results);
$rowT = $db->query("SELECT id FROM users WHERE LIMIT 10");
while ($rsT = $db->fetch_assoc($rowT)) {
$tmp_array['id'][] = $rsT;
}
$final_array[] = $tmp_array;
}## Heading ##
print_r($final_array);