Multi dimensional Array

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Taff
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 05, 2003 4:29 am
Location: Hameln(Pied Piper), Germany
Contact:

Multi dimensional Array

Postby Taff » Mon Feb 10, 2003 7:20 am

Hi,
wont pretend to know too much about this, most of wot ive got, ive got together from php.net, here and a book in a foreign language. I was making a little password checker and want to do it in an array. Under the username however, i want to have other things too, like for instance his age. So when he logs on, i check his password with his username and then just give an echo saying "you are".$age or something like that. So heres wot ive got:
$name="tom";
$allowed=array("tom"=>array("abc","12"),"dick"=>array("def","18"),"harry"=>("ghi","24"));
// i was using foreach which works fine til i go multi
foreach($allowed as $username=>$userpw)
{
if(($name==$username)AND($pw==$userpw))
{
foreach($userpw as $key=>$age)
{
echo "Hello ".$username.", you are ".$age. "years old"
}
}


Any help with the code would be greatly appreciated... or if there is an easier way of doing it?

Cheers,
Taff

Mixamajic
New php-forum User
New php-forum User
Posts: 9
Joined: Sun Feb 02, 2003 6:54 pm
Location: Milwaukee
Contact:

Try this

Postby Mixamajic » Mon Feb 10, 2003 10:33 pm

The problem is that userpw is an array and cannot be referenced directly. It will nly return and Object pointer. You can add []s to decide which element of the array you want. Here's a quick modification,

Code: Select all

$name="tom";
$allowed=array("tom"=>array("abc",12));

$name="tom";
$pw="abc";

foreach($allowed as $username=>$userpw)
{
      if(($name== $username)AND($pw == $userpw[0]))
      {
            echo "Hello ".$username.", you are ".$userpw[1]." years old";
      }
}



Hope this helps.[/code]

Taff
New php-forum User
New php-forum User
Posts: 5
Joined: Wed Feb 05, 2003 4:29 am
Location: Hameln(Pied Piper), Germany
Contact:

Postby Taff » Tue Feb 11, 2003 4:46 am

Thx a lot for your help, greatly appreciated :D
Taff


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest