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?

2
  • So if the surname is equal to foofoo you want to delete the entire array or just this element? Commented Dec 18, 2014 at 21:19
  • @Rizier entire array containing the element, in the given example it should delete the array index'd as "1" Commented Dec 18, 2014 at 21:21

1 Answer 1

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 ) )
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much thats so clear i got it right away!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.