Welcome to Knowledge Base!

KB at your finger tips

This is one stop global knowledge base where you can learn about all the products, solutions and support features.

Categories
All
Web-PHP
PHP / array_flip — DevDocs

array_flip

(PHP 4, PHP 5, PHP 7, PHP 8)

array_flip Exchanges all keys with their associated values in an array

Description

array_flip(array $array): array

array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys.

Note that the values of array need to be valid keys, i.e. they need to be either int or string . A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result .

If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

Parameters

array

An array of key/value pairs to be flipped.

Return Values

Returns the flipped array.

Examples

Example #1 array_flip() example

<?php
$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>

The above example will output:

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)

Example #2 array_flip() example : collision

<?php
$input = array("a" => 1, "b" => 1, "c" => 2);
$flipped = array_flip($input);

print_r($flipped);
?>

The above example will output:

Array
(
    [1] => b
    [2] => c
)

See Also

  • array_values() - Return all the values of an array
  • array_keys() - Return all the keys or a subset of the keys of an array
  • array_reverse() - Return an array with elements in reverse order
PHP / array_intersect — DevDocs

array_intersect

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

array_intersect Computes the intersection of arrays

Description

array_intersect(array $array, array ...$arrays): array

array_intersect() returns an array containing all the values of array that are present in all the arguments. Note that keys are preserved.

Parameters

array

The array with master values to check.

arrays

Arrays to compare values against.

Return Values

Returns an array containing all of the values in array whose values exist in all of the parameters.

Changelog

Version Description
8.0.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_intersect() example

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [a] => green
    [0] => red
)

Notes

Note : Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 . In words: when the string representation is the same.

See Also

  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_diff() - Computes the difference of arrays
  • array_diff_assoc() - Computes the difference of arrays with additional index check
Read article
PHP / array_intersect_assoc — DevDocs

array_intersect_assoc

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

array_intersect_assoc Computes the intersection of arrays with additional index check

Description

array_intersect_assoc(array $array, array ...$arrays): array

array_intersect_assoc() returns an array containing all the values of array that are present in all the arguments. Note that the keys are also used in the comparison unlike in array_intersect() .

Parameters

array

The array with master values to check.

arrays

Arrays to compare values against.

Return Values

Returns an associative array containing all the values in array that are present in all of the arguments.

Changelog

Version Description
8.0.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_intersect_assoc() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "b" => "yellow", "blue", "red");
$result_array = array_intersect_assoc($array1, $array2);
print_r($result_array);
?>

The above example will output:

Array
(
    [a] => green
)

In our example you see that only the pair "a" => "green" is present in both arrays and thus is returned. The value "red" is not returned because in $array1 its key is 0 while the key of "red" in $array2 is 1 , and the key "b" is not returned because its values are different in each array.

The two values from the key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict type check is executed so the string representation must be the same.

See Also

  • array_intersect() - Computes the intersection of arrays
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_diff() - Computes the difference of arrays
  • array_diff_assoc() - Computes the difference of arrays with additional index check
Read article
PHP / array_intersect_key — DevDocs

array_intersect_key

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

array_intersect_key Computes the intersection of arrays using keys for comparison

Description

array_intersect_key(array $array, array ...$arrays): array

array_intersect_key() returns an array containing all the entries of array which have keys that are present in all the arguments.

Parameters

array

The array with master keys to check.

arrays

Arrays to compare keys against.

Return Values

Returns an associative array containing all the entries of array which have keys that are present in all arguments.

Changelog

Version Description
8.0.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_intersect_key() example

<?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)
}

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array .

The two keys from the key => value pairs are considered equal only if (string) $key1 === (string) $key2 . In other words a strict type check is executed so the string representation must be the same.

See Also

  • array_diff() - Computes the difference of arrays
  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison
  • array_diff_assoc() - Computes the difference of arrays with additional index check
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison
Read article
PHP / array_intersect_uassoc — DevDocs

array_intersect_uassoc

(PHP 5, PHP 7, PHP 8)

array_intersect_uassoc Computes the intersection of arrays with additional index check, compares indexes by a callback function

Description

array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): array

array_intersect_uassoc() returns an array containing all the values of array that are present in all the arguments. Note that the keys are used in the comparison unlike in array_intersect() .

Parameters

array

Initial array for comparison of the arrays.

arrays

Arrays to compare keys against.

key_compare_func

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

callback(mixed $a, mixed $b): int

Return Values

Returns the values of array whose values exist in all of the arguments.

Examples

Example #1 array_intersect_uassoc() example

<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");

print_r(array_intersect_uassoc($array1, $array2, "strcasecmp"));
?>

The above example will output:

Array
(
    [b] => brown
)

See Also

  • array_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_uintersect_assoc() - Computes the intersection of arrays with additional index check, compares data by a callback function
  • array_uintersect_uassoc() - Computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison
Read article
PHP / array_intersect_ukey — DevDocs

array_intersect_ukey

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

array_intersect_ukey Computes the intersection of arrays using a callback function on the keys for comparison

Description

array_intersect_ukey(array $array, array ...$arrays, callable $key_compare_func): array

array_intersect_ukey() returns an array containing all the values of array which have matching keys that are present in all the arguments.

Parameters

array

Initial array for comparison of the arrays.

arrays

Arrays to compare keys against.

key_compare_func

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

callback(mixed $a, mixed $b): int

Return Values

Returns the values of array whose keys exist in all the arguments.

Examples

Example #1 array_intersect_ukey() example

<?php
function key_compare_func($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

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

var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func'));
?>

The above example will output:

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

In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array .

See Also

  • array_diff() - Computes the difference of arrays
  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison
  • array_diff_assoc() - Computes the difference of arrays with additional index check
  • array_diff_uassoc() - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
  • array_udiff_assoc() - Computes the difference of arrays with additional index check, compares data by a callback function
  • array_udiff_uassoc() - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
  • array_diff_key() - Computes the difference of arrays using keys for comparison
  • array_diff_ukey() - Computes the difference of arrays using a callback function on the keys for comparison
  • array_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_intersect_uassoc() - Computes the intersection of arrays with additional index check, compares indexes by a callback function
  • array_intersect_key() - Computes the intersection of arrays using keys for comparison
Read article