David Smith
David Smith

Reputation: 147

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?

Upvotes: 1

Views: 11617

Answers (3)

TBI
TBI

Reputation: 2809

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
        )

)

Upvotes: 0

Marius Behrens
Marius Behrens

Reputation: 159

You could access by $shop[0]['Title'] 0 means the first item in array and this item is also an array, which contains string keys so 'title' as second level.

To iterate it use:

//Syntax array as key => value (value is in this case also an array)
foreach($shop as $iterator_level1 => $shop_set){
    //so you can access the 2. level by string key.
    echo $shop_set['title'];
}

Hope this is helpful.

Upvotes: 0

scottlimmer
scottlimmer

Reputation: 2278

You have an array of arrays, therefore you'll need two loops.

foreach ($shop as $item) {
    foreach ($item as $key => $value) {
        echo $key;
        echo $value;
    }
}

Upvotes: 2

Related Questions