Page 1 of 1

access elements in array

Posted: Sun Mar 01, 2009 12:48 am
by matthew0786
Im new to php and im working on an array.

Here is my code

Code: Select all

 
$shop = array
(
    array
        (
        "ProductB"=>32,
        "ProductC"=>19,
        "ProductD"=>55
        ),
    array
        (
        "OpeningStock"=>55,
        "DirectCosts"=>33,
        "EndSTock"=>33
        ),
 
for ($row = 0; $row < 2; $row++)            //number of rows Stay the same
{
    echo "The row number " . $row . "</br>";
 
    foreach($shop[$row] as $key => $value)
    {
          echo $value . "</br>";
    }
 
             
}   
 
Now the above code i wrote is able to give me the values of each element. But how do i get the element names itself (e.g. ProductA, ProductB etc) these elements will be dynamic, so i cant hard code it. Or should i be using classes??

Any help is appreciated.

Re: access elements in array

Posted: Sun Mar 01, 2009 1:15 am
by php_east
echo $key."=>".$value . "</br>";

Re: access elements in array

Posted: Sun Mar 01, 2009 1:36 am
by matthew0786
Thanks! works great

Re: access elements in array

Posted: Mon Mar 02, 2009 2:09 pm
by php_east
:drunk: enjoy !

this the same very first thing that got me stuck when i started learning PHP. :)