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 3 of 3
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    foreach proper use of key values?

    Hi, I'm creating a location map, this works for multi values from db.
    PHP Code:
    while($row mysql_fetch_array$result )) {
        
    $lat=$row['lat'];
        
    $lon=$row['lon'];
    $pt getlocationcoords($lat$lon$scale_x$scale_y);
    imagefilledrectangle($im,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$dotcolor);

    problem is I need to do this with 2 pairs of lat and lon with a foreach
    not sure of the format how to do this or how to use the key/s properly

    PHP Code:
    $cord;
    $cord["lat1"] = $lat1;
    $cord["lon1"] = $lon1
    $cord
    ["dotcolor1"] = $dotcolor1;

    $cord["lat2"] = $lat2;
    $cord["lon2"] = $lon2
    $cord
    ["dotcolor2"] = $dotcolor2;

    foreach( 
    $cord as $key => $value){
    //$pt = getlocationcoords($lat, $lon, $scale_x, $scale_y);
    //imagefilledrectangle($im,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$dotcolor);

    Thanks
    Sonny

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    How did you get $lat1/$lon1 and $lat2/$lon2 to start with?
    In order to get what you want in pairs, you need to specify them as an array within the single offset of $cord.
    PHP Code:
    $cord = array(
        array(
            
    'lat' => array($lat1$lat2),
            
    'lon' => array($lon1$lon2),
            
    'dotcolor' => array($dotcolor1$dotcolor2)
        )
    );

    foreach (
    $cord AS $itm)
    {
        list(
    $firstLat$secondLat) = $itm['lat'];
        list(
    $firstLon$secondLon) = $itm['lon'];
        list(
    $firstDotColour$secondDotColour) = $itm['dotcolor'];
        
    printf('$firstLat: %s, $secondLat: %s, $firstLon: %s, $secondLon: %s, $firstDotColour: %s, $secondDotColour: %s',
            
    $firstLat$secondLat$firstLon$secondLon$firstDotColour$secondDotColour);

    That's for if you want to inter-relate multiple things together.
    For individuals at a time, you can use like so:
    PHP Code:
    $cord[0]['lat'] = $lat1;
    $cord[0]['lon'] = $lon1;
    $cord[0]['dotcolor'] = $dotcolor1;
    $cord[1]['lat'] = $lat2;
    $cord[1]['lon'] = $lon2;
    $cord[1]['dotcolor'] = $dotcolor2
    Or just traditional array building with key=>value pairs. Iteration would then have $value as an array with lat, lon, and dotcolor as valid associative indexes.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi, Fou

    Wound up trying two arrays with a combine "just a 3 line foreach", which did work, but then
    realized I needed a separate dot color as well so I finally just did this.

    PHP Code:
    $pt2 getlocationcoords($lat2$lon2$scale_x$scale_y);
    imagefilledrectangle($im,$pt2["x"]-2,$pt2["y"]-2,$pt2["x"]+2,$pt2["y"]+2,$dotcolor2); 
    and all was fine

    Sonny


  •  

    Posting Permissions

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