PHP: Reorder arrays after unset()

前端 未结 2 1639
情话喂你
情话喂你 2020-12-29 02:00

There are 2 functions involved.

  1. Search array items for a given string
  2. unset() array item if string not found

$array = a         


        
相关标签:
2条回答
  • 2020-12-29 02:28

    I think the best solution I've found is :

    Solution 1

    if you just want to remove just one element :

      array_splice($array,1,1); // all keys will be reindexed from 0
    

    where the second and third parameters are offset (key) and length (how many to remove)

    Solution 2

    The best to remove multiple keys : use array_filter() to remove all empty strings and falsey value from the array then array_splice() to reorder :

    array_splice(array_filter($array), 0, 0);
    
    0 讨论(0)
  • 2020-12-29 02:29
    $array = array_values($array);
    
    0 讨论(0)
提交回复
热议问题