PHP:从结果集填充数组

时间:2012-05-15 11:58:44

标签: php mysql arrays resultset

下面显示的代码用于手动填充数组。

    <?php
        include_once 'include/DatabaseConnector.php';   
        $data = array(
            array(0,array("111",' EE112',' AA','FT445'),"2004-03-01 10:00","2004-03-01 14:00"),
            array(1,array("111",' BC124',' RYA','FE675'),"2004-03-01 16:00","2004-03-01 18:00"),
            array(2,array("11",' BE225',' FA','AE667'),"2004-03-01 09:00","2004-03-01 10:00"),
            array(3,array("11",' TC828',' BA','FF745'),"2004-03-01 06:00","2004-03-01 08:00")
        );
?>

现在我想从数据库中填充这个数组:

$query1="SELECT * FROM MyData;";
$result1=DatabaseConnector::ExecuteQueryArray($query1);

 <?php foreach ($result1 as $row):?>
                            <tr>
                            <td><?php echo $row['resReg']; ?></td>
                            <td><?php echo $row['resTitle']; ?></td>
                            <td><?php echo $row['resAvailability'] ? 'Yes' : 'No';?></td>
                            </tr>
                            <?php endforeach;?>

如何将result1的列分配给数组的列?

1 个答案:

答案 0 :(得分:2)

我是正确的,假设你想做这样的事情:

<?php

$query1  = "SELECT name,color1,color2 FROM MyPets;";
$result1 = DatabaseConnector::ExecuteQueryArray($query1);
$petArray = array();

foreach($result1 as $pet):
  $petArray[] = array('name' => $pet['name'],
                      'colors' => array(
                           $pet['color1'],
                           $pet['color2'])
                     );
?>