Search code examples
phparraysarraylistyii2-advanced-apparray-map

How to remove particular value in array_map in php


Here below my array_map example,

$page_names = ArrayHelper::map($pagesList['data'], 'id', 'name');

Here below my array_map output,

Array
(
[2042793285968] => YoungZen Technologies
[777607709013] => Challengers
[772593172886] => Vadavalli
[152429224945] => Time Pass
)

now i want to remove 'Challengers' in the above list and i have to use the same $page_names variable for dropdown list without challengers.

I am not good in array concept, I tried like array_filter and in_array that is not happening.


Solution

  • Here is one option:

    unset($page_names[array_search('Challengers', $page_names)]);
    

    To break down the logic here, first we call array_search with the value you want to identify in your associate array. array_search will return the first key corresponding to this value, if the value can be found.

    Then, we use unset passing in the corresponding key, to completely remove that key/value pair from the array.

    To remove an entry from the map using the key, the code is much simpler:

    unset($page_names[777607709013]);