2

I have a multidimensional Array which looks like this:

Array
(
[0] => Array
    (
        [id] => 1
        [name] => Edeka Blaschek
        [description] => Lebensmittelmarkt mit Frischfleischtheke und Bäckerei Büsch
        [latitude] => 51.1178
        [longitude] => 6.77537
        [distance] => 0.18105522823916723
    )

[1] => Array
    (
        [id] => 2
        [name] => Autohaus Kopenhagen
        [description] => Peugeot Händler
        [latitude] => 51.1161
        [longitude] => 6.77481
        [distance] => 0.33650698548914737
    )

[2] => Array
    (
        [id] => 10
        [name] => Hallenbad Nievenheim
        [description] => Schwimmbad mit Sauna und Sonnenbänken, sowie kleinem Außenbereich
        [latitude] => 51.1211
        [longitude] => 6.77857
        [distance] => 0.38752806088549746
    )
)

now I want to remove the distance key and value completely from all arrays in the array. It should look like this:

    Array
(
[0] => Array
    (
        [id] => 1
        [name] => Edeka Blaschek
        [description] => Lebensmittelmarkt mit Frischfleischtheke und Bäckerei Büsch
        [latitude] => 51.1178
        [longitude] => 6.77537
    )

[1] => Array
    (
        [id] => 2
        [name] => Autohaus Kopenhagen
        [description] => Peugeot Händler
        [latitude] => 51.1161
        [longitude] => 6.77481
    )

[2] => Array
    (
        [id] => 10
        [name] => Hallenbad Nievenheim
        [description] => Schwimmbad mit Sauna und Sonnenbänken, sowie kleinem Außenbereich
        [latitude] => 51.1211
        [longitude] => 6.77857
    )
)

I've tried the solutions mentioned here: PHP - unset in a multidimensional array but they didn't worked for me, what I'm doing wrong here? Could be very simple sorry but I'm new in PHP.

EDIT: This is the code I tried:

foreach(array_keys($tempArray) as $key) {
   unset($tempArray[$key][5]);
}

And this:

foreach($tempArray as $key=>$val){
   unset($val[5]); 
}

$tempArray is the array that I want to change.

Community
  • 1
  • 1
Marcel
  • 472
  • 2
  • 6
  • 19
  • please show us the code you tried –  May 19 '17 at 02:46
  • I saw this already but the solutions didn't worked for me or I did something wrong.. – Marcel May 19 '17 at 02:52
  • I edited my question – Marcel May 19 '17 at 02:57
  • In your first snippet, you don't have a subarray with the key of `5`, so that cannot work. In the second, you are not modifying `$tempArray`, but the element within a copy of the temp array which has the key `5` (which even if you had `5`-keyed element, you would not be affecting the original `$tempArray`. – mickmackusa Jul 29 '21 at 10:19

3 Answers3

2

try this,

array_map(function($v){array_pop($v); return $v;}, $array);

Note, array_pop pop the last element of array. if the distance is not the last key, you can use unset() instead

LF00
  • 27,015
  • 29
  • 156
  • 295
1
   foreach ($mainArray as $key => $mainData){
    foreach ($subArray as $subData){ 
       if($mainData['distance'] == $subData['distance']){
        unset($mainArray[$key]); break; 
  }
   } 
     }

Try this way. –

chamina
  • 498
  • 3
  • 15
-1

Use array_values() function in php.I suppose it will works.

chamina
  • 498
  • 3
  • 15
  • I looked here http://php.net/manual/de/function.array-values.php but it seems to me that this function only shows the values of an array and doesn't filter, can you give an example please? – Marcel May 19 '17 at 02:59
  • foreach ($mainArray as $key => $mainData){ foreach ($subArray as $subData){ if($mainData['distance'] == $subData['distance']){ unset($mainArray[$key]); break; } } }Try this way. – chamina May 19 '17 at 03:48