I have an array, $arr
, which looks like this:
'sdb5' => [
'filters' => [
(int) 11 => [
'find' => [
(int) 0 => (int) 569
],
'exclude' => [
(int) 0 => (int) 89,
(int) 1 => (int) 573
]
],
(int) 86 => [
'find' => [
(int) 0 => (int) 49,
(int) 1 => (int) 522,
(int) 2 => (int) 803
],
'exclude' => [
(int) 0 => (int) 530,
(int) 1 => (int) 802,
(int) 2 => (int) 511
]
]
]
],
I've read Delete element from multidimensional-array based on value but am struggling to understand how to delete a value in an efficient way.
For example, let's say I want to delete the value 522
. I'm doing it like this:
$remove = 522; // value to remove
foreach ($arr as $filters) {
foreach ($filters as $filter) {
foreach ($filter as $single_filter) {
foreach ($single_filter as $key => $value) {
if ($value == $remove) {
unset($key);
}
}
}
}
}
I couldn't work out from the above link how to do this because even though it's a multidimensional array, it doesn't have any sub-arrays like mine.
I also don't know how else to rewrite this without repeating foreach
to get to the elements of the array I want. Again, I've read Avoid multiple foreach loops but cannot apply this to my array.
I am using PHP 7.x.
3 Answers 3
foreach()
made copy of elements. Then unset
ing the key is not enough, because you are destroying a local variable.
You could use references &
in your foreach()
loops and unset
like :
foreach ($arr as &$filters) {
foreach ($filters as &$filter) {
foreach ($filter as &$single_filter) {
foreach ($single_filter as $key => $value) {
if ($value == $remove) {
unset($single_filter[$key]);
}
}
}
}
}
Or using keys ($k1
, $k2
, ...) :
foreach ($arr as $k1 => $filters) {
foreach ($filters as $k2 => $filter) {
foreach ($filter as $k3 => $single_filter) {
foreach ($single_filter as $key => $value) {
if ($value == $remove) {
unset($arr[$k1][$k2][$k3][$key]);
}
}
}
}
}
2 Comments
foreach
statements or is there a way to write it in a shorter way? When I was looking at my code it just looked very repetitive so wasn't sure if I was being "inefficient"?array_search()
could be a more efficient way to get the key for a searched value. At least for the last loop.You could also write a recursive function, so you don't have to use nested foreach
:
function deleteRecursive(&$array, &$value) {
foreach($array as $key => &$subArray) {
if(is_array($subArray)) {
deleteRecursive($subArray, $value);
} elseif($subArray == $value) {
unset($array[$key]);
}
}
}
$valueToDelete = 522;
deleteRecursive($array, $valueToDelete);
Comments
function recursiveRemoval(&$array, $val)
{
if(is_array($array))
{
foreach($array as $key=>&$arrayElement)
{
if(is_array($arrayElement))
{
recursiveRemoval($arrayElement, $val);
}
else
{
if($arrayElement == $val)
{
unset($array[$key]);
}
}
}
}
}
Call function
recursiveRemoval($array, $value);
522
deleted and you don’t mind where, you could build a recursive loop for each array within the array.$arr
is a shortened version of the actual name). Here the user is trying to remove a filter. So my plan was to make an ajax call to a script which would then remove the filter by the value - which would be done by removing any instances of the array value from$arr
. It's stored in the session because it needs to persist between page re-loads.