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 to null would do it, but apparently it does not work.
Do the following: grep -rnw '/path/to/somewhere/' -e 'pattern' -r or -R is recursive, -n is line number, and -w stands for match the whole word. -l (lower-case L) can be added to just give the file name of matching files. -e is the pattern used during the search Along with these, --exclude, --includRead more
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it’s possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
See less
There are different ways to delete an array element, where some are more useful for some specific tasks than others. Deleting a Single Array Element If you want to delete just one single array element you can use unset() and alternatively array_splice(). By key or by value? If you know the value andRead more
There are different ways to delete an array element, where some are more useful for some specific tasks than others.
Deleting a Single Array Element
If you want to delete just one single array element you can use
unset()
and alternativelyarray_splice()
.By key or by value?
If you know the value and don’t know the key to delete the element you can use
array_search()
to get the key. This only works if the element doesn’t occur more than once, sincearray_search()
returns the first hit only.unset()
ExpressionNote: When you use
unset()
the array keys won’t change. If you want to reindex the keys you can usearray_values()
afterunset()
, which will convert all keys to numerically enumerated keys starting from 0 (the array remains a list).Example Code:
Example Output:
array_splice()
FunctionIf you use
array_splice()
the (integer) keys will automatically be reindex-ed, but the associative (string) keys won’t change — as opposed toarray_values()
afterunset()
, which will convert all keys to numerical keys.Note:
array_splice()
needs the offset, not the key, as the second parameter; offset= array_flip(array_keys(
array))[
key]
.Example Code:
Example Output:
array_splice()
, same asunset()
, take the array by reference. You don’t assign the return values back to the array.Deleting Multiple Array Elements
If you want to delete multiple array elements and don’t want to call
unset()
orarray_splice()
multiple times you can use the functionsarray_diff()
orarray_diff_key()
depending on whether you know the values or the keys of the elements to remove from the array.array_diff()
FunctionIf you know the values of the array elements which you want to delete, then you can use
array_diff()
. As before withunset()
it won’t change the keys of the array.Example Code:
Example Output:
array_diff_key()
FunctionIf you know the keys of the elements which you want to delete, then you want to use
array_diff_key()
. You have to make sure you pass the keys as keys in the second parameter and not as values. Keys won’t reindex.Example Code:
Example Output:
If you want to use
unset()
orarray_splice()
to delete multiple elements with the same value you can usearray_keys()
to get all the keys for a specific value and then delete all elements.array_filter()
FunctionIf you want to delete all elements with a specific value in the array you can use
array_filter()
.Example Code:
Example Output:
See less