-2

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

    <?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 的列分配给数组的列?

4

1 回答 1

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'])
                     );
?>
于 2012-05-15T12:19:53.857 回答