SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Array

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array

    Hi,

    I have queried database and got 2 values ($city, $country) that I need.

    Now, I want to make an array like that:

    Code PHP:
    $items = array(
    "London"=>"Great Britain",
    "Liverpool"=>"Great Britain",
    "Madrid"=>"Spain",
    "Barcelona"=>"Spain",
    ...);

    My code:
    Code PHP:
    ...
    $sql="select country, city from $table group by city";
    $items = array();
    while($row=mysql_fetch_array($result)) {
    		array_push($items, $city, $country);
    }

    How to do that?

    Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while($row=mysql_fetch_array($result)) {
            
    $items$row['city'] ] =  $row['country'];

    Be aware, that this format won't accommodate multiple countries having a city name in common. Well, neither will the sql query.

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have queried database and got 2 values ($city, $country) that I need.
    You didn't get it actually.
    the only variable you have is $row

    PHP Code:
    while($row=mysql_fetch_array($result)) {
      
    $items[$row["city"]] = $row["country"];


  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works marvelously.

    Thanks guys !


Bookmarks

Posting Permissions

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