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.

4
  • please show us the code you tried Commented May 19, 2017 at 2:46
  • I saw this already but the solutions didn't worked for me or I did something wrong.. Commented May 19, 2017 at 2:52
  • I edited my question Commented May 19, 2017 at 2: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. Commented Jul 29, 2021 at 10:19

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't worked for me, nothing has changed, the distance is already there :/
Sorry, doesn't put the result in a new variable, now it works for me, thanks!
1
   foreach ($mainArray as $key => $mainData){
    foreach ($subArray as $subData){ 
       if($mainData['distance'] == $subData['distance']){
        unset($mainArray[$key]); break; 
  }
   } 
     }

Try this way. –

Comments

-1

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

2 Comments

I looked here 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?
foreach ($mainArray as $key => $mainData){ foreach ($subArray as $subData){ if($mainData['distance'] == $subData['distance']){ unset($mainArray[$key]); break; } } }Try this way.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.