I'm trying to search through a multi-dimensional array for a value called "test4". The array looks like this:
Array
(
[0] => Array
(
[VlanId] => Array
(
[0] => 2
)
[Name] => Array
(
[0] => test2
)
)
[1] => Array
(
[VlanId] => Array
(
[0] => 3
)
[Name] => Array
(
[0] => test3
)
)
[2] => Array
(
[VlanId] => Array
(
[0] => 4
)
[Name] => Array
(
[0] => test4
)
)
I found the following posts: Search a multidimensional array php
and
using array_search() to find values in php
and I'm using the rescursiveiterator method to find the value test4. My code looks like this:
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($vlans)) as $key=>$value) {
if ($value == 'test4') {
print 'fount it. the key is: '. $key .' and value is: '. $value;
break;
}
}
This gives the following output:
fount it. the key is: 0 and value is: test4
I can't use this to unset the test4 record because [0] just unsets the first item in the outtermost array... in this case, it would remove VlanID 2, with the name test2.
can you help me figure out how to remove the record test4, once I've found it? I tried reading the following post:
Unsetting multi-dimensional arrays in php
but wasn't able to quite understand how to resolve this issue.
Thanks.
EDIT 1:
foreach ($vlans as $a=>$value) {
if ($value['Name'][0] =='test4' ){
echo 'found it at: '. $value;
unset($vlans[$a]);
break;
}
}