SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    delete array items

    I need to delete some array elements so I can then get an accurate counts of the items I will then display.

    I am getting a result set from a MySQL query and then refining those results with a Php function to cull out some of them. I then need to display a count and then display the items. I can't use the $num=@mysql_num_rows($results); because it has the count from before the Php function strips out some of the unwanted items.

    Thanks for the help.
    Jeff

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    If you're search for array elements can be written as one expression, take a look at array_filter():
    http://www.php.net/manual/en/function.array-filter.php

    PHP Code:
    function removeItems($var) {
       return (
    400 <= $bar && $bar <= 600);
    }

    $new array_filter($old'removeItems'); 
    Applied to an array the function above will only return values which are in [400,600]

    You can then use:

    PHP Code:
    count($array); 
    to get the number of elements in the array. If you have a more complex array structure, you probably must give it an index or use a loop to count.

    Christian

  3. #3
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Christian,

    I agree that array_filter is what I want to use, but I can't figure out how to use it in this circumstance.

    for each record in the first array I need to retrieve 2 values from a different table and feed these and 2 other existing values through a formula and then if the result is within a certain range allow the item to remain, else filter it out.

    Here is the function (formula) I need to run it through:
    PHP Code:
     function decimal_distance($lat1="",$lon1="",$lat2="",$lon2=""

    //$radius is determined using the following formula 
    //(360 degrees)*(60 minutes per degree)*(1.852) km per minute 
    //give a circumference of 40003.2 km 
    //radius is circumference/(2*pi) which gives us 6637km or 3956miles 
    $radius=3956
    $lat1 deg2rad ($lat1); 
    $lat2 deg2rad ($lat2); 
    $lon1 deg2rad ($lon1); 
    $lon2 deg2rad ($lon2); 
    //Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine", 
    //Sky and Telescope, vol. 68, no. 2, 1984, p. 159): 
    $dlon=$lon2-$lon1
    $dlat=$lat2-$lat1
    $sinlat=sin($dlat/2); 
    $sinlon=sin($dlon/2); 
    $a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlon*$sinlon); 
    $c=2*asin(min(1,sqrt($a))); 
    $d=$radius*$c
    // 
    return round($d); 

    the variables $lon1 and $lat1 exist from a previous query and the $lon2 and $lat2 variables would need to be retrieved for each of the items in the first result set so I can then determine whether to keep or discard them.

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    This is an example which steps are probably needed for your task.

    PHP Code:
    // loop through the array (I don't know its structure - so just guessing)

    /* Basically, I assumed following structure
     * array[0] = array('index1' => value1, ..., 'indexN' => valueN);
     * ...
     * array[n] = array('index1' => value1, ..., 'indexN' => valueN);
     *
     * example:
     * array[0] = array('lat' => 34, 'lon' => 35);
     * array[1] = array('lat' => 65, 'lon' => 12);
     * and so on
     */

    $max count($array);
    $keys array_keys($array);
    $new = array();

    for (
    $i 0$i $max$i++) {
        
    // query the table with one value of the array I took $array[$keys[$i]]['lat'].
        // if the value is not an integer you probably need single quotes around it
        
    $temp mysql_query("SELECT val1, val2 FROM sometable WHERE condition = ".$array[$keys[$i]]['lat']);

        
    // assuming that the above query only returns one row
        
    $val1 mysql_result($query00);
        
    $val2 mysql_result($query00);

        
    // test if those values are within a certain range (with $lat1, $lon1 being the existing values)
        
    $x decimal_distance($lat1$lon1$val1$val2);
        if (
    100 <= $x && $x <= 300) {
            
    // add the records to the new array
            
    $new[] = $array[$keys[$i]];
        }

    Could you post a sample of the array you want to loop through?

    Christian

  5. #5
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    while (list($key) = each($array))
    {
        if (
    preg_match('/^[\d]*$/'$key))
        {
            unset (
    $array[$key]);
        }

    that will loop through an array and delete all keys that are only numbers, and will work as long as none of your mysql column names are only numbers in which case I'd recommend using select as.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


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
  •