2015-11-10 50 views
1

我在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; 
    } 
} 

請告知

+0

這是更合適的,高性能,易使其在SQL服務器端。 –

+0

你可以實現它,並且avise –

+0

你是否第二次問同樣的問題?這是幾個小時前:[鏈接](https://stackoverflow.com/questions/33627158/loop-through-and-array-and-append-data) –

回答

0

您可以將值只是附加到$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; 
    } 
}