如何使用键在数组中附加值而不会覆盖

时间:2017-01-05 08:04:15

标签: php arrays

我在foreach循环中得到结果现在我想用键形成一个新数组,并用我的新数据形成该数组。现在,当我尝试分配数据时,它会覆盖前一个数据,因为它没有获得新的索引。到目前为止,我怎么能做到这一点:

foreach ($result as $key) {
    $pickup_location = $key->locationid;
    if (isset($pickup_location)) {
        $pickup_location = $this->db->get_where('locations', array('id' => $pickup_location ))->row();
        if (!empty($pickup_location)) {
            $supplier_dashboard['pickup_location_name'] = $pickup_location->name_en;
        }
    } 

    $dropoff_location = $key->location_dropoff;
    if (isset($dropoff_location)) {
        $dropoff_location = $this->db->get_where('locations', array('id' => $dropoff_location ))->row();
        if (!empty($dropoff_location)) { 
            $supplier_dashboard['dropoff_location_name'] = $dropoff_location->name_en;
        }
    }
    $car_make = $key->car_id;

    if (isset($car_make)) {
        $car_details = $this->db->get_where('chauffeur_rates', array('chauffeur_id' => $car_make))->row();
        if (!empty($car_details)) {
            $supplier_dashboard['car_make'] = $car_details->chauffeur_make;
        }
    }
}
return $supplier_dashboard;
}

我得到的数组是:

Array
(
 [pickup_location_name] => Seoul Downtown
 [dropoff_location_name] => Disneyland Paris
 [car_make] => makecar
)

但是我有至少7个位置名称和汽车制造而不是作为新数组添加它覆盖前一个,我应该得到结果

Array
[0](
 [pickup_location_name] => Seoul Downtown
 [dropoff_location_name] => Disneyland Paris
 [car_make] => makecar
)
Array
[1](
 [pickup_location_name] => Seoul 1
 [dropoff_location_name] => Disneyland 1
 [car_make] => makecar
)
Array
[2](
 [pickup_location_name] => Seoul 2
 [dropoff_location_name] => Disneyland 2
 [car_make] => makecar
)
... upto 7

3 个答案:

答案 0 :(得分:2)

每次循环数组的键总是与您查看结果的原因相同。你需要放一个独特的钥匙。你可以试试这个

$i = 0; //First key
foreach ($result as $key){
    $supplier_dashboard[$i]['pickup_location_name'] = $pickup_location->name_en;
    $i++; //add +1 to the key so the next element not overrides
}

答案 1 :(得分:1)

您必须将$ key添加到添加了记录的数组

使用

$supplier_dashboard[$key]['pickup_location_name'] = //your code

而不是

$supplier_dashboard['pickup_location_name'] = //your code

答案 2 :(得分:1)

您可以直接在最顶层的集合中设置值,而不是添加数组中的项目。

的Instad
$myobject['foo'] = $value;

您需要这样做(其中$iteration是您循环中的当前位置

$myobject[$iteration]['foo'] = $value;

尝试以下方法:

$carItemNumber = 0;
foreach ($result as $key) {
    $pickup_location = $key->locationid;
    if (isset($pickup_location)) {
        $pickup_location = $this->db->get_where('locations', array('id' => $pickup_location ))->row();

        if (!empty($pickup_location)) {
         $supplier_dashboard[$carItemNumber]['pickup_location_name'] = $pickup_location->name_en;
        }
    } 

    $dropoff_location = $key->location_dropoff;
    if (isset($dropoff_location)) {
        $dropoff_location = $this->db->get_where('locations', array('id' => $dropoff_location ))->row();

        if (!empty($dropoff_location)) { 
         $supplier_dashboard[$carItemNumber]['dropoff_location_name'] = $dropoff_location->name_en;
        }
    }

    $car_make = $key->car_id;

    if (isset($car_make)) {
        $car_details = $this->db->get_where('chauffeur_rates', array('chauffeur_id' => $car_make))->row();

        if (!empty($car_details)) {
         $supplier_dashboard[$carItemNumber]['car_make'] = $car_details->chauffeur_make;
        }
    }

    $carItemNumber++;
}
return $supplier_dashboard;