0

I am trying to concatenate two arrays on same level (into a new array) with keys on those values. Have tried this so far:

$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++;
                    }
                }
            }

And result i get is little off which i want it to do, the result i get is:

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
            )

    )

) I don't want this [0]=>stdClass Object while assigning, i know i'm doing $car_all_attributes[$f_key][$key] and a new key get's added and all values against it, was wondering if there's a way to do it on same level like the result array would look like this ?

[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 Answers 3

1

Instead of

$car_all_attributes[$f_key]['old_price'] = $car_fare;
$car_all_attributes[$f_key][$key] = $cars;

try this (i.e convert object into array and merge it with old price instead of adding a new key)-

$car_all_attributes[$f_key] = array_merge(
     array('old_price' => $car_fare), 
     (array) $cars
);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is spot on. Thanks alot
1

Looks like your $cars is an object. You should fetch an array instead or convert it into an array.

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++;
}

Comments

0

Change from

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

to

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.