[英]Loop through while array and add more properties to items - PHP
我在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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.