Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: array question

  1. #1
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts

    array question

    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.

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Arrays have indexes so a simple example.

    PHP Code:
    <?php

    $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.

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

    List
    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
    PHP Code:
    $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.

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

    Code:
    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.

    Code:
    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.
    Last edited by SeeIT Solutions; 06-08-2005 at 01:45 AM.

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by davehaz
    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
    PHP Code:
    $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
    then
    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)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    PHP Code:
    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

    Code:
    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

    PHP Code:
    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.

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Posts
    388
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it, thank you all.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •