www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] array, match value and return the key

  1. #1
    Join Date
    Sep 2006
    Posts
    213

    resolved [RESOLVED] array, match value and return the key

    Code:
    $list = array('1'=>'item1','2'=>'item2','3'=>'item3');
    
    $items = array('item2','item7','item9');


    How do I match $items to $list and return the keys for each match? Ive tried several array functions.

  2. #2
    Join Date
    Sep 2006
    Posts
    213
    Nevermind, I found this in the manual....

    Code:
    function array_value_intersect_keys( $array_haystack, $array_needle ){
        $intersected = array_intersect( $array_haystack, $array_needle );
        return array_keys( $intersected );
    }
    
    // usage
    
    $array_haystack = array( 1 => 2, 2 => 5, 'red' => 8, 9 => 14 );
    
    $array_needle = array( 2, 8 );
    
    $array_keys_of_intersecting_values = array_value_intersect_keys( $array_haystack, $array_needle );
    
    print_r( $array_keys_of_intersecting_values );

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles