Jump to: navigation, search

Php array intersect assoc

From w3cyberlearnings

Contents

PHP function array_intersect_assoc

This function compares two or more arrays, and it uses the first array to compare to all other arrays. Return only when the array pairs (key and value) from the first array existed in all other arrays.

Syntax array_intersect_assoc

  • array1: first array uses for the comparison
  • array2: second array
  • array3: third array
array_intersect_assoc(array1, array2, array3, ...);

Example 1

<?php
$players1 = array('mark'=>30,'john'=>40,'marry'=>45);
$players2 = array('john'=>40,'jimmy'=>30,'marry'=>45);
$players3 = array('marry'=>45,'john'=>43,'bob'=>42);

$players = array_intersect_assoc($players1, $players2, $players3);

foreach($players as $name=>$score) {
	echo "{$name} scores {$score} for all games.<br/>";
}
?>


Output

marry scores 45 for all games.

Example 2

<?php

$score1 = array(84 => 'Aaliyah', 83 => 'Cristal', 90 => 'Alaina');
$score2 = array(83 => 'Cristal', 43 => 'Alaina', 93 => 'Aaliyah');
$score3 = array(85 => 'Aaliyah', 83 => 'Cristal', 23 => 'Cristal');

$scores = array_intersect_assoc($score1, $score2, $score3);
foreach ($scores as $score => $name) {
	echo "Score {$score} in all her exams. Her name is {$name}.<br/>";
}
?>



Output

Score 83 in all her exams. Her name is Cristal.

Related Links


Navigation
Web
SQL
MISC
References