access elements in array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

access elements in array

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: access elements in array

Post by php_east »

echo $key."=>".$value . "</br>";
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

Re: access elements in array

Post by matthew0786 »

Thanks! works great
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: access elements in array

Post by php_east »

:drunk: enjoy !

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