Deleting an element from an array in PHP

后端 未结 30 4080
时光说笑
时光说笑 2020-11-21 05:55

Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?

I thought that setting it

30条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 06:51

    For associative arrays, with non-integer keys:

    Simply, unset($array[$key]) would work.

    For arrays having integer keys and if you want to maintain your keys:

    1. $array = [ 'mango', 'red', 'orange', 'grapes'];

      unset($array[2]);
      $array = array_values($array);
      
    2. array_splice($array, 2, 1);

提交回复
热议问题