View Full Version : array question

06-08-2005, 12:46 AM
howdy all,
here is a script I borrowed from php.net, you gotta love that weberdev.com toolbar add on.
//assume db connectivity
//load it all into the associative array
$sql = "SELECT key,value FROM table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result)) {
$myArray[$row[0]] = $row[1];
//now we expand it
while(list($key,$value) = each($myArray)) {
echo "$key : $value";

my question, in the select statement , what does the key and value stand for?

Also, in most everything I have read on arrays they use the $key => $value ,or something very close to it, is that putting the value of $key into $value?

I appreciate you folks answering my questions.

06-08-2005, 01:16 AM
Arrays have indexes so a simple example.


$Array = array("myKey1" => "myValue1",
"myKey2" => "myValue2"

//now say were not sure on the key values or just want to loop through them, we cant use for loop however we can use a foreach
foreach ($Array as $key => $value)
echo $key . " " . $value . "<br />"; //prints the key then value e.g. myKey1 myValue


So that should output

myKey1 myValue1
myKey2 myValue2

Now just because its $key => $value you can make it whatever you want as long as they are both different.

while(list($key,$value) = each($myArray)) {
echo "$key : $value";

List (http://php.net/list)
each (http://php.net/each)

Now basically the list command uses to give values to an Array. However we have the each function on the Array so it can only return two results key and the value. However as we are using a loop so the each function will firstly return the first set sort of like using the array_pop function where it will just pop the first array values out and remember this for the next loop which will get the 2nd, 3rd and so on.

Now just getting back to the =>

when using the => within an array

$Array = array("myKey1" => "myValue1",
"myKey2" => "myValue2"

The left side of that is the key or associate value now if you dont define it, you will get a numeric value starting from 0, the right hand side of it is the value if the => is not in the Array it is just the value since the Array will automatically set the key as a numeric value starting from 0.

Here is probably a better explaination from the manual

Syntax "index => values", separated by commas, define index and values. index may be of type string or numeric. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical index are defined, the last overwrite the first.

06-08-2005, 01:35 AM
thank you, that explains a lot, however, I have never seen key,value used in an SQL statement, what would you specifiy there, instead of key,value? You wouldn't be storing a key in a db would you? is that not something that you would create on the fly?

SeeIT Solutions
06-08-2005, 01:43 AM
a key in a database is just something unique that can identify that row from all others.

Most common key is 'id' where id = an auto incrementing number.

person name
1 fred
2 chris

in this, `person` is the key, there will be no duplicate entries therefore you can have fred known as 1 because then you can use another table like the one below and when you pull the information out of the table it goes to the previous table to find the name.

entry person age
1 2 25
2 1 30

`entry` is the key in this table. This would be useful if people changed their name so you wouldn't have to update every record in a table because their id number never changes.

06-08-2005, 01:57 AM
my question, in the select statement , what does the key and value stand for?

they are just columnnames. if you have a members-table, then the select could be

SELECT memberID, memberName FROM members

the memberID (the primary key from your member-table) would then be used as the key in the array. the membername would be the value. Your code would then look like

$sql = "SELECT memberID, memberName FROM members";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
$myArray[$row['memberID']] = $row['memberName'];
//now we expand it
foreach ($myArray as $key => $value){
echo "$key : $value";

you can think of it as a CD. the "key" is the tracknumber. the song is the "value".
CD[5] would be the same command as when you hit the track with number five in a playlist --> the associated song woud be grabbed and outputted
It's the same with arrays
Imagine your tabel contains
memberID, memberName
5, Jack
8, Bill
10, Chris
echo $myArray[5];
would echo 'Jack'
--> in the while-loop you build your playlist by adding new members (= adding a tracknumber and an associated soundfile. in this case, it would be a playlist with three songs (5,8,10))
--> in the foreach, you play your CD (= jumping from song to song and outputting the tracknumber and content)

If you would not set the key-values yourself, then they will be assigned automatically (incrementing from 0 on)

06-08-2005, 01:57 AM
SeeIT Solutions seemed to explain it pretty well. However a key from a database is the field name, although yes is unique it is actually the field name.

while($row = mysql_fetch_row($result))

Now this line here, mysql_fetch_row only return numerical arrays

array[0], array[1].

So if your table was e.g.

id | name

array[0] is the ID and name is array[0]. Unless you state the order within the Select statment

SELECT key,value FROM table

key would be array[0] and value would be array[1].

Another function similar to mysql_fetch_rows is mysql_fetch_array (http://php.net/mysql_fetch_array)

while($row = mysql_fetch_array($result))

By itself will return firstly a numerical array array[0] (key), array[1](value) etc. However also a Associate value (the field name). So array['key'], array['value'] be exactly the same as the numerical version however not needing to know the Order they are in. You can also specify if you want numerical only, associate or both which is default.

06-08-2005, 02:44 AM
got it, thank you all.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum