What is the best way to delete array item in PHP?

前端 未结 3 611
猫巷女王i
猫巷女王i 2021-02-07 20:22

Could you tell me your way to delete an item from array? Do you think it\'s good?

3条回答
  •  面向向阳花
    2021-02-07 20:31

    The common way:

    According to the manual

    unset($arr[5]); // This removes the element from the array
    

    The filtered way:

    There is also the array_filter() function to take care of filtering arrays

    $numeric_data = array_filter($data, "is_numeric");
    

    To get a sequential index you can use

    $numeric_data = array_values($numeric_data);
    

    References
    PHP – Delete selected items from an array

提交回复
热议问题