Forum Moderators: coopster

Message Too Old, No Replies

PHP 5 assigning array data to variables

php 5 assigning array data to variables

         

MrWhippy

10:12 am on Mar 26, 2009 (gmt 0)

10+ Year Member



Hi all,

I am currently having a problem assigning data that is in an array to variables.

I have tried using both extract and referencing the array directly.
The array is set as shown below and does include the data it should do.

here is the code i am using, thanks.


$user = array($logondetails->get_user());

//attempt one using extract
extract($user);

//attempt two using direct reference of array, have both numeric and string reference in here have tried both exclusively also.
$username = $user[2];
$password = $user[pwd];
$email= $user[email];
$first= $user[first];
$last= $user[last];
$club= $user[club];
$tel= $user[tel];


The output method to show what i actually get is shown below with its results

//output result of which is shown below
echo $username."<br>";
echo $email."<br>";
echo $first_name."<br>";
print_r($user);
echo "<br>".$user[2];

result of above I have stripped the notices out of this if you want to see the exact screen please let me know.

Steve

Array ( [0] => Array ( [0] => 9 [user_id] => 9 [1] => Steve [username] => Steve [2] => 098f6bcd4621d373cade4e832627b4f6 [pwd] => 098f6bcd4621d373cade4e832627b4f6 [3] => steve@fusion-tec.co.uk [email] => steve@example.com [4] => Steve [first_name] => Steve [5] => Whipday [surname] => Whipday [6] => 0 [tel] => 0 [7] => 1 [club_id] => 1 [8] => [active_url] => [9] => 0 [confirmed_email] => 0 [10] => 0 [active] => 0 ) )

I am sure it must be something simply that i am missing but cant think of it as the array is set and Username is set also which is shown by Steve printing to screen.

Thanks in advance.

[edited by: MrWhippy at 10:13 am (utc) on Mar. 26, 2009]

[edited by: eelixduppy at 5:27 pm (utc) on Mar. 26, 2009]
[edit reason] formatting [/edit]

cameraman

11:17 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look very carefully at this:
Array ( [0] => Array ( [0] => 9 [user_id] => 9

You've got two levels of array there. To get to user_id, for example:
echo $user[0]['user_id'];

It's because of this line:
$user = array($logondetails->get_user());

The array() [us3.php.net] function creates an array and you're already expecting an array from your get_user member function. Try this:
$user = $logondetails->get_user();

and I think you'll get the results you're expecting to see.

MrWhippy

11:31 am on Mar 26, 2009 (gmt 0)

10+ Year Member



Thanks im just in a meeting ill try that as soon as i can

MrWhippy

11:38 am on Mar 26, 2009 (gmt 0)

10+ Year Member



cameraman you are an absolute legend,

I thought it would be something simple and i tried something similar to what you have just suggested a before i put the array in front and it didnt work so i put array in there.

I did think the output was very strange from the print_r but couldnt think what the problem was. its working now thanks

can i rate a user on here give you a thumbs up a big up for the post? if so how not sure how if you tell me you will get one.

cameraman

11:42 am on Mar 26, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LOL you just did, thanks - it's always the little things that hang you up, glad you're rolling again.