Search code examples
phparraysechoprevar-dump

PHP:: cannot retrieve all values inside array


Here is my simple code:

<?php

$components=array(
1 => 'Carrot', 'Apple', 'Orange',
2 => 'Boiled Egg', 'Omelet',
3 => 'Ice Cream', 'Pancake', 'Watermelon'
);

echo'<pre>';
var_dump($components);
echo'</pre>';

output :

array(6) {
  [1]=>
  string(6) "Carrot"
  [2]=>
  string(10) "Boiled Egg"
  [3]=>
  string(9) "Ice Cream"
  [4]=>
  string(6) "Omelet"
  [5]=>
  string(7) "Pancake"
  [6]=>
  string(10) "Watermelon"
}
  1. Where is 'Apple' & 'Orange' ?
  2. Why I cannot retrieve the a specific value from (for example : $components[1][2] = 'r') :/ Why ?!

Solution

  • Make an array like this,

    $components=array(
      1 => ['Carrot', 'Apple', 'Orange'],
      2 => ['Boiled Egg', 'Omelet'],
      3 => ['Ice Cream', 'Pancake', 'Watermelon']
    );
    

    And now check your array.