Remove any element of PHP array by using unset, array_diff() or by searching for value & using key

preview_player
Показать описание
We can use unset() function in PHP to remove any element from the array. This unset() function takes the key of the element as input and remove both key and value from the array. After removal the key and value remains same and no rearrangement is done.
Using value to remove the element
We will use array_diff() to get the difference between main array and another or second array. In the second array we will keep the element we want to remove. We can use more than one element also to delete.
$remove=array(c,f);
$new_array=array_diff($input,$remove);

Most of the time we will remove the element by using the value. So we will search the value by using array_search(). This function array_search() will return the key of the matched value. Using this value we can remove the element by using unset().
unset($input[array_search('d',$input)]);

#unsetarray #array_diff #array_serarch
Рекомендации по теме
Комментарии
Автор

Wow! Well done 😉
Any chance to hide a field based on user role?

stefanpfadt
visit shbcf.ru