我在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
中,项目1以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;
}
}