Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    If inside multi array, return array

    I'm using:

    PHP Code:
    function in_array_multi($needle$haystack) {
        foreach (
    $haystack as $item) {
            if (
    $item === $needle || (is_array($item) && in_array_multi($needle$item))) {
                return 
    true;
            }
        }
        return 
    false;

    If the needle is in the 2nd array, return the name of the array.
    IE:

    $candy = array("chocolate" => array("snicker","musketeer"));

    if musketeer is found, return chocolate

    How can I do this? If not possible, how with just an associative array?
    Last edited by RonnyNishimoto; 07-29-2012 at 09:16 AM.

  • #2
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    I figured it out:

    PHP Code:
    if (array_search($v$array) == true) { // $v in associative
        
    $draft array_search($v$array);
    }
    foreach (
    $array2 as $key => $value) { // $v in multi-dimensional
        
    if (in_array($v$value)) {
            
    $v $key;
        }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •