PHP Arrays

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
geoff
Forum Newbie
Posts: 3
Joined: Sat May 17, 2008 12:32 am

PHP Arrays

Post by geoff »

Hi i have a array which is populated from a mysql query, It selects values from 3 columns in the table. I was first wondering how to retrieve say the id column value from the array. When i do a print_r(array_values($members)); line it comes out with this: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) ). I was wondering why it spits out like this when all the examples i have seen come out without array then another array.

So i was wondering how i can retrieve the id from the array and also why my array is printing out the way it is.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: PHP Arrays

Post by Kieran Huggins »

try using the form:

Code: Select all

$result = MySQL_query("SELECT id, row1, row2 FROM sometable...") or die(MySQL_error());
while($row = MySQL_fetch_assoc($result)){
  // do stuff per row here, like
  echo $row['id'];
}
Does that answer your question? Or did I misinterpret?
geoff
Forum Newbie
Posts: 3
Joined: Sat May 17, 2008 12:32 am

Re: PHP Arrays

Post by geoff »

yeah that does help, but the query i have is in an query page where is performs the query and brings back one result which is $members = get_login($username);. After this i then want to go through the completed queries array $member and pick out the id but it doesn't allow me because the array when printed looks like this: Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => jt [password] => jt ) ) and when i use the line to get the id it comes up blank.
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: PHP Arrays

Post by nowaydown1 »

I think something else is going on if the array you are getting back from that function is indeed multi-dimensional. Kieran suggested the use of mysql_fetch_assoc because it looks like your array is giving you both numeric indexes and associative indexes. This is typical of a mysql_fetch_array call with the result type of MYSQL_BOTH specified (which is default). I find it a little less confusing to use MYSQL_ASSOC, or switch your mysql_fetch_array call to mysql_fetch_assoc as Kieran suggested. Up to you.

I would guess the additional zero based index is because you're shoving every row onto a new index. Normally when that happens your code is something like:

Code: Select all

 
while($row = mysql_fetch_array($result)) {
    $newArray[] = $row;
}
 
At any rate, if you just wanted to access the ID parameter in your multi-dimensional array just use:

Code: Select all

 
$idValue = $members[0]["id"];
 
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP Arrays

Post by dbemowsk »

Looking at the array you have posted;

Code: Select all

Array ( [0] => Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) )
I am not sure what the layout is of your database table is, but to me this looks like the result of mysql_fetch_array($result, MYSQL_BOTH), and my guess is that you have the following fields; id, username, and password. The first part of the array "Array ( [0] => Array (..." is the result of the returned row. Since you are looking to return only one row in the result, you may want to look into using something like this:

Code: Select all

 
$result = mysql_query("SELECT * FROM user_data WHERE id = $id LIMIT 1");
if ($results) {
    //Personally I prefer mysql_fetch_assoc which just returns the associative array portion of the result set like this
    //list($members) = mysql_fetch_assoc($result);
    //but there may be an advantage to using the result the way it is below
    list($members) = mysql_fetch_array($result, MYSQL_BOTH);
}
print_r($members);
 
Doing it this way will result in:

Code: Select all

Array ( [0] => 2 [id] => 2 [1] => thommo [username] => thommo [2] => abc [password] => abc ) )
allowing you to use either $members[0] or $members['id'] to get the ID, $members[1] or $members['username'] to get the username, etc...

The key to this is the PHP list function. If you do not use the list function and just use this:

Code: Select all

$data = mysql_fetch_array($result, MYSQL_BOTH);
You would then need to reference $members[0][0] or $members[0]['id'] to get the ID and $members[0][1] or $members[0]['username'] to get the username, etc... Where $members[0] is the first and only row result.

using mysql_fetch_assoc with the list function would result in the following array:

Code: Select all

Array ( [id] => 2 [username] => thommo [password] => abc ) )
This would allow you to use $members['id'], $members['username'] or $members['password'] to reference any of the 3 fields.

Hope this explanation was helpful.
Post Reply