Php 如何构建多维数组?

Php 如何构建多维数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我使用foreach循环从数据库值创建数组,如下所示: foreach ($query->result_array() as $row) { array( 'user_id' => $user_id, 'post_id' => $row['id'], 'time' => '0', 'platform' => $platform ); } 假设我拉2行,我需要让这个foreach创建一个多维数组

我使用foreach循环从数据库值创建数组,如下所示:

foreach ($query->result_array() as $row) {
   array(
      'user_id'  => $user_id,
      'post_id'  => $row['id'],
      'time'     => '0',
      'platform' => $platform
   );
}
假设我拉2行,我需要让这个foreach创建一个多维数组,格式如下:

$data = array(
    array(
       'user_id'  => '12', 
       'post_id'  => '37822', 
       'time'     => '0',
       'platform' => 'email'
    ),
    array(
       'user_id'  => '12', 
       'post_id'  => '48319', 
       'time'     => '0',
       'platform' => 'email'
    ),
);

可能很简单,只是还是记不下来。谢谢。

您可以先声明一个空数组:

$results = array();
然后,每次有新行时,将其添加到该数组中:

$results[] = $row;
$results[] = array( something here );
或者,无论如何,要向该数组中添加任何内容:

$results[] = $row;
$results[] = array( something here );

在您的特定情况下,您可能会使用以下内容:

$results = array();
foreach ($query->result_array() as $row) {
    $results[] = array(
                    'user_id' => $user_id, 
                    'post_id' => $row['id'], 
                    'time' => '0', 
                    'platform' => $platform
                );
}

作为参考,PHP手册的相应部分:.

$data[]=array(您已经拥有的);