About creating an array at php.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manolis
    New Member
    • Sep 2010
    • 1

    About creating an array at php.

    Hi,

    I am having trouble at creating a specific array.
    What i want is to pull the info for my members (from mysql database) and then store them to an array.
    The array should be like this:
    Code:
    $members = array(
    'John' => array('avatar' => '/images/avatar/ji.jpg', 'country' => 'uk.'),
    'Nick' => array('avatar' => '/images/avatar/nick.jpg', 'country' => 'italy.'),
    );
    etc..
    so i pull the name,avatar url and country from the db and then i store them in to the previous array.
    My question is, how could i create this array?

    Thanks in advance!
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    You query the database and then run a while loop on the results. Then dump the row information into an array called users.

    Below's an example.

    Code:
    $result = mysql_query("SELECT name,avatar,url,country FROM mytable");
    
    $users = array();
    while ($row = mysql_fetch_array($result)) {
        $users[$row["name"]] = $row; 
    }
    Last edited by Dormilich; Sep 6 '10, 05:44 AM.
    niheel @ bytes

    Comment

    Working...