繁体   English   中英

遍历while数组并向项目添加更多属性-PHP

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM