1

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?

Community
  • 1
  • 1
mrahmat
  • 617
  • 6
  • 21
  • So if the `surname` is equal to `foofoo` you want to delete the entire array or just this element? – Rizier123 Dec 18 '14 at 21:19
  • @Rizier entire array containing the element, in the given example it should delete the array index'd as "1" – mrahmat Dec 18 '14 at 21:21

1 Answers1

1

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 ) )
Rizier123
  • 58,877
  • 16
  • 101
  • 156