2

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?

5
  • $shop[0]['Price'] == 1.25 where no key is set the array is indexed numerically, otherwise you can access values via it's key value. Commented Jul 14, 2014 at 13:51
  • array_column or a forach. Or at least tell us what you've tried. Commented Jul 14, 2014 at 13:51
  • I think in this case you could use eg: $shop[0]['title']. As you haven't given your outer array a string index, it can only be index by a number. But your inner one's can be indexed by strings. I think this is what your asking? Commented Jul 14, 2014 at 13:51
  • mario you may need to scroll down abit on the code. I've commented the things Ive tried to access them, what I was expecting and what I got Commented Jul 14, 2014 at 13:54
  • That's right, so $shop[0]['title'] worked. But how to you get the key "title" and it's values in a foreach loop? Commented Jul 14, 2014 at 13:58

3 Answers 3

2

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok this one seems to make sense. Because it is an array of arrays. You must first loop through first set of arrays, within that then loop through the second set of array at which point I can access the actual key not the index position?
1

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.

Comments

0

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
        )

)

8 Comments

Tried this and it worked. How did you get the actual keys to transfer across? I can understand you specified $value['Title'] as being the value. But I do not see you specifying $shop[0]['Title']? Plus you changed the $shop as $value, so only values were available. Yet just by stating ['Title'], no reference to $value or $shop for title?
$shop[0]['Title'] will give you only value of first element in array. If you are using foreach you dont need to specify key on everytime. As per your requirement, you want 'Title' as key for newarray. So, i have given 'Title' as key of $newarray by declaring an array '$newarray[]['Title']'.
tried adding another field $newarray[]['Number'] = $value['Number']; instead of adding it to the existing arrays, it created a new index number for it
Ah right, so it is not dynamically taking the title from $shop, you have explicitly given the name to the field as being 'Title' is that correct?
yeah, i have given it explicitly but we could give it dynamically by using $key.
|

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.