Array Functions in PHP: PART 5

Introduction

In this article I describe the PHP array functions array_flip, array_intersect and array_intersect_assoc. To learn some other math functions, go to:

  1. Array Functions in PHP: PART 1
     
  2. Array Functions in PHP: PART 2
     
  3. Array Functions in PHP: PART 3
     
  4. Array Functions in PHP: PART 4

PHP array_flip  Function

The array _flip function is used to exchange all keys with their associated values in an array.

Syntax

array_flip (array)

Parameter

The parameter for the array_flip function is:

Parameter Description
array It specifies an array.

Example

An example of the array_flip function is:

<?php

echo"<pre>";

$val=array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");

$exchange = array_flip($val);

print_r($exchange);

?>

Output

array-flip-function-in-php.jpg 

PHP array_intersect Function

The array_intersect function computes the intersection of an array and returns an array with the key and value from first array but the values and keys are present in all of the other array.

Syntax
 

array_intersect(array1, array2,................)

Parameter

The parameters for the array_intersect function are:

Parameter Description
array1 It specifies an array, who is compare from others.
array2 It  specifies an array to compare against first array.
................ It specifies more array to compare against first array.

Example

An example of the array_intersect function is:

<?php

echo "<pre>";

$val = array("a" => "Apple", "b" => "Mango", "c" => "Orange", "Banana");

$val2 = array("a" => "Apple", "b"=>"Raspberry", "banana");

$interscet = array_intersect($val,$val2);

print_r($interscet);

?>

Output

array-intersect-function-in-php.jpg

PHP array_intersect_assoc Function


The array_intersect_assoc function computes the intersection of an array with additional checks and returns an array with the key and value from the first array but the values and keys are present in all of the other array.

Syntax

array_intersect_assoc(array1, array2,................)

Parameter

The parameters for the array_intersect_assoc function are:

Parameter Description
array1 It specifies an array, who is compare from others.
array2 It  specifies an array to compare against first array.
................ It specifies more array to compare against first array.

Example

An example of the array_intersect_assoc function is:

<?php
echo
"<pre>";
$
val = array(1 => "Apple", 2 => "Mango", 3 => "Orange", "Banana");
$val2 = array(1 => "Apple", 2=>"Raspberry", "banana");
$interscet = array_intersect_assoc($val,$val2);
print_r($interscet);

?>


Output

array-intersect-assoc-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all