array_intersect_assoc


php128 apg

COMPUTES the intersection of ARRAYS with additional index check.





This function returns an ARRAY containing all the values from $array1 that are present in any of the other arrays.

Multiple occurrences in $array1 are all treated the same way.

Note that the keys are also used in the comparison unlike in the function array_intersect.



<?php

arr array_intersect_assoc 
arr $array1 arr $array2 [, 
                                          
arr $arra3, ..., arr $arrayN ] )
 

where,

$array1 The first ARRAY to check

$array2 
The second ARRAY to compare against

$array3 
The third ARRAY to compare against

. . . . . . . . . . . . . . . .

$arrayN The last ARRAY to compare against

?>
 

$array1


The first ARRAY - values to check.



$array2


The second ARRAY - to compare values against.



$array3


The third ARRAY - to compare values against.



$arrayN


The last ARRAY - to compare values against.



  1 EXERCISE   

<?php

$arr_current 
= [ "Lust""Gluttony"
                                    
"Greed""Sloth"
                                    
"Wrath""Envy""Pride"];

$arr_gregory = [ "Pride""Envy"
                                     
"Anger""Sadness"
                                     
"Avarice""Gluttony""Lust" ];

$arr_thomas = [ "Pride""Greed"
                                     
"Gluttony""Lust"
                                     
"Sloth""Envy""Anger" ];

$arr_common array_intersect_assoc($arr_current
                                                  
$arr_gregory
                                                  
$arr_thomas);

echo 
'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';

print_r($arr_current);

echo 
'<br><br>ACCORDING POPE GREGORY:<br>';

print_r($arr_gregory);

echo 
'<br><br>ACCORDING ST. THOMAS AQUINAS:<br>';

print_r($arr_thomas);

echo 
'<br><br><br>COMMON AT ALL:<br>';

print_r($arr_common);

?> 

 RESULT   

SEVEN DEADLY SINS.

CURRENT:
Array ( [0] => Lust [1] => Gluttony [2] => Greed [3] => Sloth [4] => Wrath [5] => Envy [6] => Pride )

ACCORDING POPE GREGORY:
Array ( [0] => Pride [1] => Envy [2] => Anger [3] => Sadness [4] => Avarice [5] => Gluttony [6] => Lust )

ACCORDING ST. THOMAS AQUINAS:
Array ( [0] => Pride [1] => Greed [2] => Gluttony [3] => Lust [4] => Sloth [5] => Envy [6] => Anger )


COMMON AT ALL:
Array ( )

Keys and values of the first array were checked against the others.

All keys and values associations have been verified.

No keys and values matching, an empty array was returned.


  2 EXERCISE   

<?php

$Acurrent 
= [ 'first' => "Lust"'second' => "Gluttony",
           
'third' => "Greed"'fourth' => "Sloth"'fifth' => "Wrath",
                       
'sixth' => "Envy"'seventh' => "Pride"];

$Agregory = [ 'first' => "Pride"'second' => "Envy"
                  
'third' => "Anger"'fourth' => "Sadness"
                  
'fifth' => "Avarice"'sixth' => "Gluttony"
                               
'seventh' => "Lust" ];

$Athomas = [ 'first' => "Pride"'second' => "Greed"
                        
'third' => "Gluttony"'fourth' => "Lust"
                        
'fifth' => "Sloth"'sixth' => "Envy",
                                      
'seventh' => "Anger" ];

$Acommon array_intersect_assoc($Acurrent
                                                        
$Agregory
                                                        
$Athomas);

echo 
'SEVEN DEADLY SINS.<br><br>CURRENT:<br>';

print_r($Acurrent);

echo 
'<br><br>ACCORDING POPE GREGORY:<br>'

print_r($Agregory);

echo 
'<br><br>ACCORDING ST. THOMAS AQUINAS:<br>'

print_r($Athomas);

echo 
'<br><br><br>COMMON AT ALL:<br>'

print_r($Acommon);

?> 

 RESULT   

SEVEN DEADLY SINS.

CURRENT:
Array ( [first] => Lust [second] => Gluttony [third] => Greed [fourth] => Sloth [fifth] => Wrath [sixth] => Envy [seventh] => Pride )

ACCORDING POPE GREGORY:
Array ( [first] => Pride [second] => Envy [third] => Anger [fourth] => Sadness [fifth] => Avarice [sixth] => Gluttony [seventh] => Lust )

ACCORDING ST. THOMAS AQUINAS:
Array ( [first] => Pride [second] => Greed [third] => Gluttony [fourth] => Lust [fifth] => Sloth [sixth] => Envy [seventh] => Anger )


COMMON AT ALL:
Array ( )

Keys and values of the first array were checked against the others.

All keys and values associations have been verified.

No keys and values matching, an empty array was returned.


  3 EXERCISE   

<?php

/*
* Testing the behavior of array_intersect()
* by passing array with
* binary values for $arr1 and $arr2 argument.
*/

echo "Testing array_intersect_assoc() : binary safe checking.<br><br>";

// array with binary values
$arr_binary = array(b"hello"b"world");
// simple array
$arr_normal = array("hello""world");

echo 
'The given $arr_binary:<br>';
var_dump($arr_binary);

echo 
'<br><br>The given $arr_normal:<br>';
var_dump($arr_normal);


// array with binary value for $arr1 argument
echo '<br><br>After 
    array_intersect_assoc($arr_binary, $arr_normal):<br>'
;
var_dumparray_intersect_assoc($arr_binary$arr_normal) );

// array with binary value for $arr2 argument
echo '<br><br>After 
    array_intersect_assoc($arr_normal, $arr_binary):<br>'
;
var_dumparray_intersect_assoc($arr_normal$arr_binary) );

// array with binary value for both $arr1 and $arr2 argument
echo '<br><br>After 
    array_intersect_assoc($arr_binary, $arr_binary):<br>'
;
var_dumparray_intersect_assoc($arr_binary$arr_binary) );

?>

  4 EXERCISE   

<?php

/*
* Testing the behavior of 
* array_intersect_assoc()
* by passing 2-D arrays
* to both $arr1 and $arr2 argument.
* Optional argument takes the same value
* as that of $arr1
*/

echo "Testing array_intersect_assoc() : 
              passing two dimensional array 
              to both \$arr1 and \$arr2 arguments.<br><br>"
;

// two dimensional arrays for $arr1 and $arr2 argument
$arr1 = array (

  
// arrays with default keys
  
array(12"hello"'world'),
  array(
1234),

  
// arrays with explicit keys
  
array(=> "one"=> "two"=> "three"),
  array(
"ten" => 10"twenty" => 20.00"thirty" => 30)
);

$arr2 = array (
  array(
1234),
  array(
=> "one"=> "two"=> "three")
);

echo 
'The given $arr1:<br>';
var_dump($arr1);

echo 
'<br><br>The given $arr2:<br>';
var_dump($arr2);



/* Passing the entire array 
              as argument to $arr1 and $arr2 */
// Calling array_intersect_assoc() 
// with default arguments
echo "<br><br>Passing the 
                 entire 2-D array to \$arr1 and \$arr2.<br>"
;
echo 
"With default arguments:<br>";
var_dumparray_intersect_assoc($arr1$arr2) );

// Calling array_intersect_assoc() 
// with more arguments
// additional argument passed is the same as $arr1
echo "<br><br>With more arguments:<br>";
var_dumparray_intersect_assoc($arr1$arr2$arr1) );

/* Passing the sub-array as argument
                        to $arr1 and $arr2 */
// Calling array_intersect_assoc() 
// with default arguments
echo "<br><br>Passing the sub-array to \$arr1 and \$arr2.<br>";
echo 
"<br><br>With default arguments:<br>";
var_dumparray_intersect_assoc($arr1[0], $arr2[0]) );

// Calling array_intersect_assoc() 
// with more arguments
// additional argument passed 
// is the same as $arr1
echo "<br><br>With more arguments:<br>";
var_dumparray_intersect_assoc($arr1[0], 
                                                     
$arr2[0], 
                                                     
$arr1[0]) );

?>

  5 EXERCISE   

<?php

/*
 * Testing the functionality of 
 * array_intersect_assoc() 
 * by passing different
 * associative arrays having 
 * different possible keys
 * to $arr2 argument.
 * The $arr1 argument passed 
 * is a fixed array.
*/

echo "Testing array_intersect_assoc() : 
                 assoc array with diff keys 
                            to \$arr2 argument."
;

// get an unset variable
$unset_var 10;
unset (
$unset_var);

// get a heredoc string
$heredoc = <<<EOT
Hello world
EOT;

// different variations of associative arrays
// to be passed to $arr2 argument
$arrays = array (

       
// empty array
/*1*/  
array(),

       
// arrays with integer keys
       
array(=> "0"),
       array(
=> "1"),
       array(
=> "1"=> "2"=> "3"=> "4"),

       
// arrays with float keys
/*5*/  
array(2.3333 => "float"),
       array(
1.2 => "f1"3.33 => "f2",
             
4.89999922839999 => "f3",
             
33333333.333333 => "f4"),

       
// arrays with string keys
/*7*/  
array('\tHello' => 111're\td' => "color",
             
'\v\fworld' => 2.2'pen\n' => 33),
       array(
"\tHello" => 111"re\td" => "color",
             
"\v\fworld" => 2.2"pen\n" => 33),
       array(
"hello"$heredoc => "string"),
       
// heredoc

       // array with  unset variable
/*10*/ 
array( @$unset_var => "hello"),

       
// array with mixed keys
/*11*/ 
array('hello' => 1"fruit" => 2.2,
              
133 => "int"444.432 => "float",
             @
$unset_var => "unset"
             
$heredoc => "heredoc")
);

// array to be passsed to $arr1 argument
$arr1 = array(=> 0=> "float"=> "f3"
              
33333333 => "f4",
              
"\tHello" => 1112.2'color'
              
"Hello world" => "string",
              
"pen\n" => 33133 => "int");

// loop through each sub-array 
// within $arrays
// to check the behavior of 
// array_intersect_assoc()
$iterator 1;
foreach(
$arrays as $arr2) {
  echo 
"<br><br>Iteration: $iterator<br>";

  
// Calling array_intersect_assoc() 
  // with default arguments
  
var_dumparray_intersect_assoc($arr1$arr2) );

  
// Calling array_intersect_assoc() 
  // with more arguments.
  // additional argument passed 
  // is the same as $arr1 argument
  
var_dumparray_intersect_assoc($arr1$arr2$arr1) );
  
$iterator++;
}

?>

  6 EXERCISE   

<?php

/*
 * Testing the functionality 
 * of array_intersect_assoc() 
 * by passing different
 * associative arrays having 
 * different possible keys 
 * to $arr1 argument.
 * The $arr2 argument 
 * passed is a fixed array
*/

echo "Testing array_intersect_assoc() : 
                 assoc array with diff keys 
                             to \$arr1 argument."
;

// get an unset variable
$unset_var 10;
unset (
$unset_var);

// get a heredoc string
$heredoc = <<<EOT
Hello world
EOT;

// different variations of 
// associative arrays 
// to be passed to $arr1 argument
$arrays = array (

       
// empty array
/*1*/  
array(),

       
// arrays with integer keys
       
array(=> "0"),
       array(
=> "1"),
       array(
=> "1"=> "2"=> "3"=> "4"),

       
// arrays with float keys
/*5*/  
array(2.3333 => "float"),
       array(
1.2 => "f1"3.33 => "f2",
             
4.89999922839999 => "f3",
             
33333333.333333 => "f4"),

       
// arrays with string keys
/*7*/  
array('\tHello' => 111're\td' => "color",
             
'\v\fworld' => 2.2'pen\n' => 33),
       array(
"\tHello" => 111"re\td" => "color",
             
"\v\fworld" => 2.2"pen\n" => 33),
       array(
"hello"$heredoc => "string"),
       
// heredoc

       // array with object, unset variable 
       // and resource variable
/*10*/ 
array(@$unset_var => "hello"),

       
// array with mixed keys
/*11*/ 
array('hello' => 1"fruit" => 2.2,
              
133 => "int"444.432 => "float",
             @
$unset_var => "unset"
             
$heredoc => "heredoc")
);

// array to be passsed to $arr2 argument
$arr2 = array(=> 0=> "float"=> "f3"
              
33333333 => "f4",
              
"\tHello" => 1112.2'color'
              
"Hello world" => "string",
              
"pen\n" => 33,  133 => "int");

// loop through each sub-array within 
// $arrays to check the behavior of 
// array_intersect_assoc()
$iterator 1;
foreach(
$arrays as $arr1) {
  echo 
"<br><br>Iteration: $iterator<br>";

  
// Calling array_intersect_assoc() 
  // with default arguments
  
var_dumparray_intersect_assoc($arr1$arr2) );

  
// Calling array_intersect_assoc() 
  // with more arguments.
  // additional argument passed 
  // is the same as $arr1 argument
  
var_dumparray_intersect_assoc($arr1$arr2$arr1) );
  
$iterator++;
}

?>

  7 EXERCISE   

<?php

/*
* Passing different types of arrays 
* to $arr2 argument and testing whether
* array_intersect_assoc() behaves 
* in an expected way with the other 
* arguments passed to the function.
* The $arr1 argument passed 
* is a fixed array.
*/

echo "Testing array_intersect_assoc() : 
             Passing different types of arrays 
                      to \$arr2 argument."
;

/* Different heredoc strings passed as argument to $arr2 */
// heredoc with blank line
$blank_line = <<<EOT


EOT;

// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The big brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;

// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string. 
With\vdifferent\fwhite\vspaces
EOT;

// heredoc with quoted strings 
// and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;

// array to be passsed 
// to $arr1 argument
$arr1 = array (
11.11.3=> true"hello""one"NULL2,
'world'truefalse=> "b\tbbb""aaaa\r",
$numeric_string"h3" => $diff_whitespaces"true" => true,
"one" => "ten"=> "four""two" => 2=> "six",
''null => "null"'' => 'emptys'
);

// arrays to be passed 
// to $arr2 argument
$arrays = array (
/*1*/  array(12),
// array with default keys 
// and numeric values
array(1.11.21.3),
// array with default keys 
// and float values
array(false,true),
// array with default keys 
// and boolean values
array(),
// empty array
/*5*/  
array(NULL),
// array with NULL
array("a\v\f","aaaa\r","b","b\tbbb",
          
"c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),
          
// array with double quoted strings
array('a\v\f','aaaa\r','b','b\tbbb',
          
'c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),
          
// array with single quoted strings
array($blank_line"h2" => $multiline_string
                     
"h3" => $diff_whitespaces
                     
$numeric_string),
          
// array with heredocs

// associative arrays
/*9*/  
array(=> "one"=> "two"=> "six"),
// explicit numeric keys, string values
array("one" => 1"two" => 2"three" => ),
// string keys & numeric values
array( => 10=> 20=> 40=> 30),
// explicit numeric keys and numeric values
array( "one" => "ten""two" => "twenty"
                
"three" => "thirty"),
// string key/value
array("one" => 1=> "two"=> "four"),  //mixed

// associative array, containing
// null/empty/boolean values 
// as key/value
/*14*/ 
array(NULL => "NULL"null => "null"
                                    
"NULL" => NULL
                                    
"null" => null),
array(
true => "true"false => "false"
                                
"false" => false
                                
"true" => true),
array(
"" => "emptyd"'' => 'emptys'
                               
"emptyd" => ""
                               
'emptys' => ''),
array(
=> ''=> ""=> NULL=> null
                    
=> false=> true),
array(
'' => 1"" => 2NULL => 3
                         
null => 4false => 5
                         
true => 6),

// array with repetitive keys
/*19*/ 
array("One" => 1"two" => 2
                     
"One" => 10"two" => 20
                                   
"three" => 3)
);

// loop through each sub-array 
// within $arrays to check the behavior 
// of array_intersect_assoc()
$iterator 1;
foreach(
$arrays as $arr2) {
echo 
"<br><br>Iteration: $iterator<br>";

// Calling array_intersect_assoc() 
// with default arguments
var_dumparray_intersect_assoc($arr1$arr2) );

// Calling array_intersect_assoc()
// with more arguments
// additional argument passed 
// is the same as $arr1 argument
var_dumparray_intersect_assoc($arr1$arr2$arr1) );
$iterator++;
}

?>

  8 EXERCISE   

<?php

/*
* Passing different types of arrays 
* to $arr1 argument and testing whether
* array_intersect_assoc() behaves 
* in an expected way with the other arguments
* passed to the function
* The $arr2 argument passed is a fixed array.
*/

echo "Testing array_intersect_assoc() : 
                Passing different types of arrays 
                to \$arr1 argument."
;

/* Different heredoc strings passed as argument to $arr1 */
// heredoc with blank line
$blank_line = <<<EOT


EOT;

// heredoc with multiline string
$multiline_string = <<<EOT
hello world
The big brown fox jumped over;
the lazy dog
This is a double quoted string
EOT;

// heredoc with different whitespaces
$diff_whitespaces = <<<EOT
hello\r world\t
1111\t\t != 2222\v\v
heredoc\ndouble quoted string. 
With\vdifferent\fwhite\vspaces
EOT;

// heredoc with quoted strings and numeric values
$numeric_string = <<<EOT
11 < 12. 123 >22
'single quoted string'
"double quoted string"
2222 != 1111.\t 0000 = 0000\n
EOT;

// arrays to be passed to $arr1 argument
$arrays = array (
/*1*/  array(12),
       
// with default keys and numeric values
       
array(1.12.2), 
       
// with default keys & float values
       
array(false,true), 
       
// with default keys and boolean values
       
array(), 
       
// empty array
/*5*/  
array(NULL), 
       
// with NULL
       
array("a\v\f","aaaa\r","b","b\tbbb",
                      
"c","\[\]\!\@\#\$\%\^\&\*\(\)\{\}"),  
       
// with double quoted strings
       
array('a\v\f','aaaa\r','b','b\tbbb',
                      
'c','\[\]\!\@\#\$\%\^\&\*\(\)\{\}'),  
       
// with single quoted strings
       
array("h1" => $blank_line"h2" => $multiline_string
                             
"h3" => $diff_whitespaces
                                            
$numeric_string),  
       
// with heredocs

       // associative arrays
/*9*/  
array(=> "one"=> "two"=> "three"),  
       
// explicit numeric keys, string values
       
array("one" => 1"two" => 2"three" => ),  
       
// string keys & numeric values
       
array( => 10=> 20=> 40=> 30),  
       
// explicit numeric keys and numeric values
       
array( "one" => "ten""two" => "twenty"
                                 
"three" => "thirty"),  
       
// string key/value
       
array("one" => 1=> "two"=> "four"),  
       
//mixed

       // associative array, containing 
       // null/empty/boolean values as key/value
/*14*/ 
array(NULL => "NULL"null => "null""NULL" => NULL
                                        
"null" => null),
       array(
true => "true"false => "false"
                         
"false" => false"true" => true),
       array(
"" => "emptyd"'' => 'emptys'
                              
"emptyd" => ""'emptys' => ''),
       array(
=> ''=> ""=> NULL=> null
                                
=> false=> true),
       array(
'' => 1"" => 2NULL => 3null => 4
                                     
false => 5true => 6),

       
// array with repetitive keys
/*19*/ 
array("One" => 1"two" => 2"One" => 10
                                       
"two" => 20"three" => 3)
);

// array to be passsed to $arr2 argument
$arr2 = array (
  
11.12.2"hello""one"NULL2,
  
'world'true,=> false=> 'aaaa\r'"aaaa\r",
  
'h3' => $diff_whitespaces$numeric_string,
  
"one" => "ten"=> "four""two" => 2,
  
''null => "null"'' => 'emptys'"emptyd" => "",
);

// loop through each sub-array 
// within $arrays to check the behavior
// of array_intersect_assoc()
$iterator 1;
foreach(
$arrays as $arr1) {
  echo 
"<br><br>Iteration: $iterator<br>";

  
// Calling array_intersect_assoc() 
  // with default arguments
  
var_dumparray_intersect_assoc($arr1$arr2) );
  echo 
'<br><br>';
  
// Calling array_intersect_assoc() 
  // with more arguments.
  // additional argument passed
  //  is the same as $arr1 argument
  
var_dumparray_intersect_assoc($arr1$arr2$arr1) );
  
$iterator++;
}

echo 
"Done";
?>

  9 EXERCISE   

<?php

/*
* Testing the behavior of 
* array_intersect_assoc() 
* by passing different arrays for the arguments.
* Function is tested by passing 
* associative array as well 
* as array with default keys.
*/

echo "Testing array_intersect_assoc() : 
                      basic functionality.<br><br>"
;

// array with default keys
$arr_default_keys = array(12"hello"'world');

// associative array
$arr_associative = array("one" => 1"two" => 2);

echo 
'The given $arr_default_keys:>br>';
var_dump($arr_default_keys);

echo 
'<br><br>The given $arr_associative:<br>';
var_dump($arr_associative);

// default key array for both 
// $arr1 and $arr2 argument
echo '<br><br>
After 
 array_intersect_assoc($arr_default_keys, 
                                   $arr_default_keys):<br>'
;
var_dumparray_intersect_assoc($arr_default_keys
                                                    
$arr_default_keys) );

// default key array for $arr1 
// and associative array for $arr2 argument
echo '<br><br>
After 
 array_intersect_assoc($arr_default_keys, 
                                    $arr_associative):<br>'
;
var_dumparray_intersect_assoc($arr_default_keys
                                                    
$arr_associative) );

// associative array for $arr1 
// and default key array for $arr2
echo '<br><br>
After 
 array_intersect_assoc($arr_associative, 
                                   $arr_default_keys):<br>'
;
var_dumparray_intersect_assoc($arr_associative
                                                     
$arr_default_keys) );

// associative array for both $arr1 
// and $arr2 argument
echo '<br><br>After 
  array_intersect_assoc($arr_associative, 
                                   $arr_associative):<br>'
;
var_dumparray_intersect_assoc($arr_associative
                                                    
$arr_associative) );

// more arrays to be intersected
$arr3 = array(234);
echo 
'<br><br><br>The given $arr3:<br>';
var_dump($arr3);

echo 
'<br><br>After
 array_intersect_assoc($arr_default_keys, 
                                    $arr_associative, 
                                    $arr3):<br>'
;
var_dumparray_intersect_assoc($arr_default_keys
                                                    
$arr_associative
                                                    
$arr3) );

echo 
'<br><br>After 
 array_intersect_assoc($arr_associative, 
                                    $arr_default_keys, 
                                    $arr3, 
                                    $arr_associative):<br>'
;
var_dumparray_intersect_assoc($arr_associative
                                                     
$arr_default_keys
                                                     
$arr3
                                                     
$arr_associative) );

?>