简体   繁体   中英

PHP var_dump array

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) "myEmail@domain.com"
  ["emailAddress"]=>
  string(24) "myEmail@domain.com"
  [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)

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) "myEmail@domain.com"

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) "myEmail@domain.com"

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 .

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

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 "myEmail@domain.com"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM