0

I am not sure what I am doing wrong here and would appreciate the help

I have an array the looks like this:

Array
(
    [85369] => 3
    [85368] => 13
    [85378] => 23
)

When I try to update the database with

update_post_meta( $id, 'key', $MyArr);

in the databse the array is not stored. What is stored is:

a:0:{}

Any help would be appreciated

Full Code:

function distance($lat1, $lng1, $lat2, $lng2, $unit) {
$theta = $lng1 - $lng2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
    if($unit == "K"){
    return round ( ($miles * 1.609344) );
    }elseif ($unit == "N"){
    return round ( ($miles * 0.8684) );
    }else{
    return round ( $miles );
    }
}

$MilesArray = array();
$bounding_distance = 1;
$nearbys = $wpdb->get_results( 
        "
         SELECT *  
         FROM rvty_geodata
         WHERE (
                geo_latitude BETWEEN ($lat - $bounding_distance) AND ($lat + $bounding_distance) AND geo_longitude BETWEEN ($lng - $bounding_distance) AND ($lng + $bounding_distance) 
            )
        "
);  

foreach ($nearbys as $nearby) {
    $alt_id = $nearby->post_id;
    $alt_lat = $nearby->geo_latitude;
    $alt_lng = $nearby->geo_longitude;
    if ($rvparks->ID != $alt_id) {
        $Miles = distance($lat, $lng, $alt_lat, $alt_lng, "M");
        $MilesArray[$alt_id] = $Miles;
    }
} 
asort($MilesArray);
if(count($MilesArray)>10){
    $MilesArrayChunk = array_chunk($MilesArray, 10, true);
    $ALTCGArray = $MilesArrayChunk[0];
 }else{
    $ALTCGArray = $MilesArray;
}
update_post_meta( $rvparks_id, 'alt_camps', $ALTCGArray);
  • 1
    We need to see your full code. – kaiser Aug 1 '13 at 2:05
  • Full code added. – Kenny Scott Aug 2 '13 at 16:29
  • Are you sure that you have anything in $MilesArray variable (that it's not an empty array)? – Krzysiek Dróżdż Aug 2 '13 at 16:50
  • Yes, I do. I have used the print_r function to test and it prints out the very top of my initial question. – Kenny Scott Aug 2 '13 at 17:30
  • @Krzysiek, You were right... I had this in two areas. The area that printed out the array had all the needed variables. he second which was doing the saving was missing a variable and nothing was being passed ont o the function. Thank you very much! – Kenny Scott Aug 2 '13 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.