Joao Miesler Joao Miesler - 1 year ago 110
PHP Question

Array inside array in php

I'm developing a webapplication where there is a page that you can see the listed items that you placed on the website.

When I fetch some results from the DB I can end up with two cars that he/she listed. Which will end up something like this:

carID: 1

  • model: R8

  • Brand: Audi

  • Color: Yellow

carID: 2

  • Model: Audi TT

  • Brand: Audi

  • Color: White

When I try use the function while
it will overwrite the last
and it will never end up with two cars. Here is my code:

$car_id_array = $carid;
$final_array = array($car_id_array => array(

"brand" => $brand,
"color" => $color,
"type" => $type,
"price" => $price,
"mileage" => $mileage,
"model" => $model,
"year" => $year




foreach($final_array as $final_array_key => $final_array_value){

foreach($final_array_value as $car_id_array_key => $car_id_array_value){

echo $final_array_key . " ----> " . $car_id_array_key . ":" . $car_id_array_value;
echo "<br>";


I would like to end up with the two
into the

Answer Source

Why are you using two arrays when you can end up using only indexes?

Try this code:

        $final_array[$i] = array(
            "carid"     => $carid,
            "brand"     => $brand,
            "color"     => $color,
            "type"      => $type,
            "price"     => $price,
            "mileage"   => $mileage,
            "model"     => $model,
            "year"      => $year          


        foreach ($final_array as $key => $value){
            echo $key . ":" . $value;
            echo "<br>";
        echo "<br>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download