Search code examples
phparraysunset

Remove keys without specified names


I have an array like this:

Array
(
    [44] => 2
    [21] => 2
    [] => 2
    [27] => 2
)

How to find and remove (unset) keys without specified names? So in this case array should look like this:

Array
(
    [44] => 2
    [21] => 2
    [27] => 2
)

Solution

  • could be the key is '' (empty string)

    in this case assuming you have

       $myArray = [
        [44] => 2,
        []   => 2,
        [21] => 2,
        [27] => 2,
       ]
    

    then try unset

     unset($myArray['']);