Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Computes the intersection of arrays using keys for comparison

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 107
    Comment on it

    If you want to get an array which contain all the entries of an array and have keys that are present in some other arrays for this you can use a predefined function array_intersect_key ( array $array1 , array $array2 [, array $... ] ).
    array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all other arguments.

    Here is a example which will explain this function-

    <?php
    $array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);
    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);
    
    var_dump(array_intersect_key($array1, $array2));
    ?>
    

    The above example will output:

    array(2) {
      ["blue"]=> int(1)
      ["green"]=> int(3)
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: