phpwordpressunset

Unset() value from PHP Array To Prevent Print Out To Screen


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


Solution

  • 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);