-2

I'm a jr developer working on a wordpress plugin that uses an API to retrieve data for the site. Within the API call are latitude and longitude values for the Google maps API on the same page. There is a for loop to print each provided value from the API. I would like to stop this loop from printing the provided latitude and longitude values to the screen while still providing those values to the Google Map. A few solutions come to mind but I'm not certain which is the best solution... 1.) Unset() the latitude and longitude values at the point of the for loop. Perhaps this will prevent it from applying the values to the Google Map API?

2.) Within the for loop set an if statement to watch for those two values - but this would be checking for the values every time one of the many fields are looped through, does this solution take away from the performance/speed of the website?

Thank you in advance

mudpain
  • 3
  • 2
  • 1
    Hi can you provide some more details on question example data array how you retrieve from api(Only need the data structure) some dummy data. Also what you meat my print out to the screen? So once it is apply to map you need removed it. I'm try to understand you question bit. Not sure if wont to do some thing like this. <<< print_r($arr) foreach($arr as $k => $v) { unset($arr[$k]); } print_r($arr)>>> – Amila Priyankara Nov 15 '21 at 18:43
  • The API payload has several values we want to echo onto the website for any user to see with their browser. Here is a small sample of the values... `[BathsFull] => 2, [BathsHalf] => 1, [BathsTotal] => 3, [BedsTotal] => 4, [Latitude] => 96.794636, [Longitude] => -96.828915, [ListPrice] => 729900` As I previously mentioned, a for loop prints everything from that array (API) I don't want to print the latitude and longitude values to the web page, but I still want to be able to provide those values to Google Maps API – mudpain Nov 15 '21 at 18:59

1 Answers1

0

I hope array map is what you looking for

$in=[
[
    'BathsFull' => 2, 
    'BathsHalf' => 1, 
    'BathsTotal' => 3, 
    'BedsTotal' => 4, 
    'Latitude' => 96.794636, 
    'Longitude' => -96.828915, 
    'ListPrice' => 729900
 ],
 [
    'BathsFull' => 2, 
    'BathsHalf' => 1, 
    'BathsTotal' => 3, 
    'BedsTotal' => 4, 
    'Latitude' => 96.794636, 
    'Longitude' => -96.828915, 
    'ListPrice' => 729900
 ]
];
print_r($in);
$out = array_map(function (array $arr) {unset($arr['Longitude'],$arr['Latitude']);return $arr;}, $in);
print_r($out);
Amila Priyankara
  • 118
  • 1
  • 12