我在PHP中运行查询,遍历所有项并将它们添加到数组中;

$select_all_restaurants = mysqli_query($connection, $query);
$rows = $select_all_restaurants -> num_rows;

$arr = array();
if($rows > 0) {
    while($rows = mysqli_fetch_assoc($select_all_restaurants)) {
        $arr[] = $rows;
    }
}


我如何从另一个查询以及数组中的每个项目将数据附加到$arr中。
因此,如果item1在第一个查询中具有属性id,name,则当我运行第二个查询时,我想向其添加更多属性,例如distance。因此在$arr中,item1以id,name,distance结尾

我正在获取另一组数据的查询如下;

$info = get_driving_information($address1, $address2);
echo $info['distance'];
echo $info['time'];


我也从原始查询中得到$address1

这就是我尝试过的;

$select_all_restaurants = mysqli_query($connection, $query);
$rows = $select_all_restaurants -> num_rows;

$arr = array();
if($rows > 0) {
    while($rows = mysqli_fetch_assoc($select_all_restaurants)) {

$info = get_driving_information($rows['address1'], $address2);
// I get two properties from this query
$info['distance'];
$info['time'];

// How do I add these 2 properties for every item in $arr?
//
        $arr[] = $rows;
    }
}


请指教

最佳答案

您可以将值附加到$rows对象,例如

$arr = array();
if($rows > 0) {
    while($rows = mysqli_fetch_assoc($select_all_restaurants)) {
        $info = get_driving_information($rows['address1'], $address2);
        // I get two properties from this query
        $rows['distance'] = $info['distance'];
        $rows['time'] =  $info['time'];
        $arr[] = $rows;
    }
}

10-04 11:55
查看更多