tags:

views:

195

answers:

4
array(14) {
  [0]=>
  string(1) "1"
  ["id"]=>
  string(1) "1"
  [1]=>
  string(7) "myUserName"
  ["UserID"]=>
  string(7) "myUserName"
  [2]=>
  string(10) "myPassword"
  ["passwordID"]=>
  string(10) "myPassword"
  [3]=>
  string(24) "[email protected]"
  ["emailAddress"]=>
  string(24) "[email protected]"
  [4]=>
  string(7) "myFirstName"
  ["firstName"]=>
  string(7) "myFirstName"
  [5]=>
  string(8) "myLastName"
  ["lastName"]=>
  string(8) "myLastName"
  [6]=>
  string(1) "1"
  ["active"]=>
  string(1) "1"
}

how do i access the contents of this array using PHP?

the above was a var_dump($info)

+2  A: 

You can use either the numeric or the associative key:

echo $array[0]; // outputs 1
echo $array["id"];  // outputs 1

I'm guessing this is the result of a mysql_fetch_array() operation, isn't it? You may want to specify whether you want a numeric or associative array using the second parameter to that function.

Example:

$record = mysql_fetch_array($query, MYSQL_ASSOC);  // for associative keys only
$record = mysql_fetch_array($query, MYSQL_NUM);    // for numeric keys only
Pekka
+1 : I've edited my answer to put a link to yours ; nice catch, about the `mysql_fetch_array` stuff !
Pascal MARTIN
+3  A: 

It depends on which part of the array you are trying to access.


If you are trying to access a specific item, you can access it by its index ; for instance :

echo $info['passwordID'];

Should give you :

myPassword


(edit after the comment)

For the email address, there is this portion in your var_dump's output :

["emailAddress"]=>
  string(24) "[email protected]"

This indicates that the e-mail address is stored in the array as an element that has the key "emailAddress".

Which means you should be able to get that e-mail address like this :

echo $info['emailAddress'];


And as you also have this portion of text in the var_dump's output :
(About that duplication of data, you should read Pekka's answer, who provides an idea of why your data is in your array twice, with both integers and strings as keys)

[3]=>
  string(24) "[email protected]"

You could also use :

echo $info[3];

(of course, in each of those cases, you could also store this to a variable for futures re-use)


Another solution, if you want to access each item, would be to use some foreach loop ; for instance :

foreach ($info as $key => value) {
    echo "Value for key $key is $value <br />";
}


You might want to go through the arrays section of the PHP manual, for more informations.

And, also, the section about array functions.

Pascal MARTIN
I'm trying to get the email address and I cant get it to print
orange
Using Martin's approach, you should be able to figure it out.
Anthony Forloney
@orange : I've edited my answer ; and as I said during my edit, you should also read Pekka's answer, who explains *"why"* you're probably getting this kind of data.
Pascal MARTIN
+1 once again, the most detailed answer in da house! :) If that doesn't help the OP, nothing will.
Pekka
Thanks :-) Yep, hope it'll help :-)
Pascal MARTIN
A: 

The array appears to have both string and numeric keys. You can access the fields using the array index operator []. Supply either the numeric key or the column name:

echo $info['UserID']; // output "myUserName"
echo $info['emailAddress']; // output "[email protected]"
meagar
A: 

if $info is the array, then you can echo $info[6] for example.
If you want it as a string then $s=print_r($info,true);

It seems you are doing some thing wrong, as there shouldn't be a need to access array like that, and there other ways to access objects as arrays.

Itay Moav