I have the following script for a Google maps application. The information about a specific place is sent via ajax to the $array array which is then added to the $SESSION['infomarker']. This part works fine. But the user should also be able to delete a place (If they regret the input).
This is handled by the removeElementWithValue() function, which takes the places lat and lng as arguments ($val1 and $val2 in this sample script). The array which contains the 'val1' and 'val2' values then disappears, like they should. But when I change the $val1 and $val2 variables to 'val3' and 'val4', the array which contained 'val1' and 'val2' returns while the array which contains 'val3' and 'val4' is unset.
I thought that unset() 'unset' the array in question for good, or do I need to do something else?
<?php
session_start();
if(isset($_POST['lat']) && !empty($_POST['lat']) ) {
$array = array(
'titel' => $_POST['titel'],
'comment' => $_POST['comment'],
'lat' => $_POST['lat'],
'lng' => $_POST['lng']
);
$_SESSION['infomarker'][] = $array;
}
$val1 = 'val1';
$val2 = 'val2';
$newarray = removeElementWithValue($_SESSION['infomarker'], "titel", "comment", $val1, $val2);
function removeElementWithValue($array, $key1, $key2, $value1, $value2){
foreach($array as $subKey => $subArray){
if($subArray[$key1] == $value1) {
if($subArray[$key2] == $value2)
unset($array[$subKey]);
}
}
return $array;
}
print_r($newarray);