Search code examples
phparraysmultidimensional-array

PHP arrays, how to access keys and values


Trying to learn multidimensional arrays but seem to constantly struggling with accessing them. I still have not got grasped how you access them using index, keys, values.

How do I get to the actual word "Title" and it's value?

Here I have one I was playing with.

$shop = array( array( "Title" => "rose", 
                      "Price" => 1.25,
                      "Number" => 15 
                    ),
               array( "Title" => "daisy", 
                      "Price" => 0.75,
                      "Number" => 25,
                    ),
               array( "Title" => "orchid", 
                      "Price" => 1.15,
                      "Number" => 7 
                    )
             );

Which prints a structure such as this:

Array
(
    [0] => Array
        (
            [Title] => rose
            [Price] => 1.25
            [Number] => 15
        )

    [1] => Array
        (
            [Title] => daisy
            [Price] => 0.75
            [Number] => 25
        )

    [2] => Array
        (
            [Title] => orchid
            [Price] => 1.15
            [Number] => 7
        )

)

echo $shop[0][0][0]; //I Expect "rose" but I get "Undefined offset: 0"
echo $shop['Price']; //I expect 1.25 but I get "Undefined index: Price"

foreach($shop as $key=>$value)
{
echo $key; //I expect the key values "Title/Price/Number" instead I get Index numbers 0 1 2
echo $value; //I expect all values of keys e.g. "rose",1.25,15/"daisy",0.75,25/"orchid",1.15,7 Instead I get Array to string conversion error
}

What I am trying to do, is take all the title and value from the shop array, and put it into a new array called $x = array(); and then take a car key/value from a different array and combine them together.

so the new array ends up looking like this:

Array
(
    [0] => Array
        (
            [Title] => rose //from $shop array
            [Car] => Mercedez //from $car array
        )

    [1] => Array
        (
            [Title] => daisy //from $shop array
            [Car] => Ford //from $car array
        )

    [2] => Array
        (
            [Title] => orchid //from $shop array
            [Car] => Bentley //from $car array
        )

)

Also is there a way to access the actual name of the key "title" and not a index number?


Solution

  • Try this -

    $newarray = array();
    foreach($shop as $key=>$value) {
        $newarray[$key]['Title'] = $value['Title'];
        $newarray[$key]['Number'] = $value['Number'];
    }
    echo "<pre>";print_r($newarray);
    

    Here, $newarray will give you output like this.

    Array
    (
        [0] => Array
            (
                [Title] => rose
                [Number] => 15
            )
    
        [1] => Array
            (
                [Title] => daisy
                [Number] => 25
            )
    
        [2] => Array
            (
                [Title] => orchid
                [Number] => 7
            )
    
    )