在php codeigniter中的concatinating数组

时间:2017-01-02 09:57:34

标签: php arrays codeigniter

我正在尝试使用这些值上的键在同一级别(进入新数组)中连接两个数组。到目前为止已经尝试过这个:

$car_all_attributes = array();
            foreach ($cars_without_price as $key => $cars) 
            {
                $keep_fare = 0;
                $i = 0;
                foreach ($data['cars_fare'] as $f_key => $fare) 
                {
                    $c_fare = (explode("-",$fare));
                    if ($c_fare[0] == $cars->chauffeur_id) 
                    {
                        $car_fare = $c_fare[1];
                        $car_all_attributes[$f_key]['old_price'] = $car_fare;
                        $car_all_attributes[$f_key][$key] = $cars;
                        $i++;
                    }
                }
            }

我得到的结果是我希望它做的很少,我得到的结果是:

Array
(
[0] => Array
    (
        [old_price] => 460
        [0] => stdClass Object
            (
                [chauffeur_id] => 16
                [chauffeur_avalibality] => 1
                [chauffeur_supplier_id] => 1
                [chauffeur_country_id] => 190
                [chauffeur_city_id] => 0
                [chauffeur_make] => Mercedes S 350 or similar
                [chauffeur_model] => 
                [chauffeur_car_type] => 2
                [chauffeur_transmission] => 
                [chauffeur_door] => 4
                [chauffeur_passengers] => 5
                [chauffeur_large_suitecase] => 4
                [chauffeur_small_suitecase] => 5
                [chauffeur_ac] => 1
                [chauffeur_img] => carType_145851015451.jpg
                [chauffeur_service_chaufer] => 1
                [chauffeur_service_airport] => 0
                [deleted] => 0
            )

    )

[1] => Array
    (
        [old_price] => 352
        [1] => stdClass Object
            (
                [chauffeur_id] => 17
                [chauffeur_avalibality] => 1
                [chauffeur_supplier_id] => 1
                [chauffeur_country_id] => 190
                [chauffeur_city_id] => 0
                [chauffeur_make] => Mercedes E-Class or similar
                [chauffeur_model] => 
                [chauffeur_car_type] => 1
                [chauffeur_transmission] => 
                [chauffeur_door] => 4
                [chauffeur_passengers] => 4
                [chauffeur_large_suitecase] => 4
                [chauffeur_small_suitecase] => 4
                [chauffeur_ac] => 0
                [chauffeur_img] => carType_145851017815.jpg
                [chauffeur_service_chaufer] => 1
                [chauffeur_service_airport] => 0
                [deleted] => 0
            )

    )

[2] => Array
    (
        [old_price] => 368
        [2] => stdClass Object
            (
                [chauffeur_id] => 18
                [chauffeur_avalibality] => 1
                [chauffeur_supplier_id] => 1
                [chauffeur_country_id] => 190
                [chauffeur_city_id] => 0
                [chauffeur_make] => Mercedes Viano or similar
                [chauffeur_model] => 
                [chauffeur_car_type] => 3
                [chauffeur_transmission] => 
                [chauffeur_door] => 4
                [chauffeur_passengers] => 6
                [chauffeur_large_suitecase] => 4
                [chauffeur_small_suitecase] => 6
                [chauffeur_ac] => 1
                [chauffeur_img] => carType_145851020593.jpg
                [chauffeur_service_chaufer] => 1
                [chauffeur_service_airport] => 0
                [deleted] => 0
            )

    )

) 分配时我不希望这个[0]=>stdClass Object,我知道我正在做$car_all_attributes[$f_key][$key]并且添加了一个新的密钥并且所有的值都反对它,想知道是否有办法在同一级别上执行此操作结果数组看起来像这样吗?

[0] => Array
    (
                [old_price] => 460
                [chauffeur_id] => 16
                [chauffeur_avalibality] => 1
                [chauffeur_supplier_id] => 1
                [chauffeur_country_id] => 190
                [chauffeur_city_id] => 0
                [chauffeur_make] => Mercedes S 350 or similar
                [chauffeur_model] => 
                [chauffeur_car_type] => 2
                [chauffeur_transmission] => 
                [chauffeur_door] => 4
                [chauffeur_passengers] => 5
                [chauffeur_large_suitecase] => 4
                [chauffeur_small_suitecase] => 5
                [chauffeur_ac] => 1
                [chauffeur_img] => carType_145851015451.jpg
                [chauffeur_service_chaufer] => 1
                [chauffeur_service_airport] => 0
                [deleted] => 0

3 个答案:

答案 0 :(得分:1)

看起来您的if ($c_fare[0] == $cars->chauffeur_id) { $car_fare = $c_fare[1]; $car_all_attributes[$f_key] = (array)$cars; $car_all_attributes[$f_key]['old_price'] = $car_fare; $i++; } 是一个对象。您应该取代数组或将其转换为数组。

if(isset($_POST['Send']))
{

    $name = $_POST['name'];
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $errors = array();

    if($name == "")
    {
         $errors['error'] = "Töltsd ki a mezőt(1)!";
    }
    if($to == "")
    {
        $errors['error_mail'] = "Töltsd ki a mezőt(2)!";
    }
    // Email cim ellenőrzése
    if (!filter_var($to, FILTER_VALIDATE_EMAIL))
    {
           $errors['error_mail_valid'] = "Hibás email cím!";
    }
    if($subject == "")
    {

        $errors['error_subj'] = "Töltsd ki a mezőt(3)!";
    }
    if($message == "")
    {
        $errors['error_message'] = "Töltsd ki a mezőt(4)!";
    }
    if ($name != "" && $to != "" && $subject != "")
    {
        echo "Email elküldve";

        $name = "";
        $to = "";
        $subject = "";
        $message = "";
    }

}

答案 1 :(得分:1)

而不是

$car_all_attributes[$f_key] = array_merge(
     array('old_price' => $car_fare), 
     (array) $cars
);

试试这个(即将对象转换为数组并将其与旧价格合并而不是添加新密钥) -

{{1}}

答案 2 :(得分:0)

改变
$car_all_attributes[$f_key][$key] = $cars;

$car_all_attributes[$f_key] = (array)$cars;