0

我需要创建一个带有 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 循环,第二个我需要做 while,因为它是一个 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
4

1 回答 1

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);
于 2013-10-18T14:09:22.093 回答