I have a multidimensional array of the structure :
$_SESSION['array'] = array(1=>array("surname"=>"foofoo", "name"=>"foo"),2=> .... so on);
I want to delete an entry if the surname matches a given variable e.g
$surname = "foofoo";
the function should search the entire arrays if the $surname was found, delete that array
I tried looking at some answers like the one given at here and here but I could not understand them clearly, can someone show a clear method along with some good explanations and if possible links to some documentations for reading?
This should work for you:
(In this code i go trough each innerArray and each value & key from the innerArray. Then i simple check if it's the right key with the right value. If the condition is true i unset the entire array)
<?php
$_SESSION['array']= array(1=>array("surname"=>"foofoo", "name"=>"foo"), 2=>array("surname"=>"foofoo2", "name"=>"foo2"));
foreach($_SESSION['array']as $innerArrayKey => $innerArray) {
foreach($innerArray as $k => $v) {
if($k == "surname" && $v == "foofoo")
unset($_SESSION['array'][$innerArrayKey]);
}
}
print_r($array);
?>
Output:
Array ( [2] => Array ( [surname] => foofoo2 [name] => foo2 ) )