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_diff — DevDocs

array_diff

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

array_diff Computes the difference of arrays

Description

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

Compares array against one or more other arrays and returns the values in array that are not present in any of the other arrays.

Parameters

array

The array to compare from

arrays

Arrays to compare against

Return Values

Returns an array containing all the entries from array that are not present in any of the other arrays. Keys in the array array are preserved.

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_diff() example

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

print_r($result);
?>

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

Example #2 array_diff() example with non-matching types

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2 . That is, when the string representation is the same.

<?php
// This will generate a Notice that an array cannot be cast to a string.
$source = [1, 2, 3, 4];
$filter = [3, 4, [5], 6];
$result = array_diff($source, $filter);

// Whereas this is fine, since the objects can cast to a string.
class S {
  private $v;

  public function __construct(string $v) {
    $this->v = $v;
  }

  public function __toString() {
    return $this->v;
  }
}

$source = [new S('a'), new S('b'), new S('c')];
$filter = [new S('b'), new S('c'), new S('d')];

$result = array_diff($source, $filter);

// $result now contains one instance of S('a');
?>

To use an alternate comparison function, see array_udiff() .

Notes

Note :

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]); .

See Also

  • array_diff_assoc() - Computes the difference of arrays with additional index check
  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison
  • array_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check

PHP / array_diff_assoc — DevDocs

array_diff_assoc

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

array_diff_assoc Computes the difference of arrays with additional index check

Description

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

Compares array against arrays and returns the difference. Unlike array_diff() the array keys are also used in the comparison.

Parameters

array

The array to compare from

arrays

Arrays to compare against

Return Values

Returns an array containing all the values from array that are not present in any of the other arrays.

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_diff_assoc() example

In this example you see the "a" => "green" pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair 0 => "red" is in the output because in the second argument "red" has key which is 1 .

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

The above example will output:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Example #2 array_diff_assoc() example

Two values from key => value pairs are considered equal only if (string) $elem1 === (string) $elem2 . In other words a strict check takes place so the string representations must be the same.

<?php
$array1 = array(0, 1, 2);
$array2 = array("00", "01", "2");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [0] => 0
    [1] => 1
)

Notes

Note : This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]); .

Note : Ensure you pass arguments in the correct order when comparing similar arrays with more keys. The new array should be the first in the list.

See Also

  • array_diff() - Computes the difference of arrays
  • 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_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
Read article

PHP / array_diff_key — DevDocs

array_diff_key

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

array_diff_key Computes the difference of arrays using keys for comparison

Description

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

Compares the keys from array against the keys from arrays and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Parameters

array

The array to compare from

arrays

Arrays to compare against

Return Values

Returns an array containing all the entries from array whose keys are absent from all of the other arrays.

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_diff_key() example

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.

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

var_dump(array_diff_key($array1, $array2));
?>

The above example will output:

array(3) {
  ["blue"]=>
  int(1)
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}
<?php
$array1 = array('blue' => 1, 'red'  => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'yellow' => 7, 'cyan' => 8);
$array3 = array('blue' => 6, 'yellow' => 7, 'mauve' => 8);

var_dump(array_diff_key($array1, $array2, $array3));
?>

The above example will output:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

Notes

Note :

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_key($array1[0], $array2[0]); .

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_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
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison
Read article

PHP / array_diff_uassoc — DevDocs

array_diff_uassoc

(PHP 5, PHP 7, PHP 8)

array_diff_uassoc Computes the difference of arrays with additional index check which is performed by a user supplied callback function

Description

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

Compares array against arrays and returns the difference. Unlike array_diff() the array keys are used in the comparison.

Unlike array_diff_assoc() a user supplied callback function is used for the indices comparison, not internal function.

Parameters

array

The array to compare from

arrays

Arrays to compare 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 an array containing all the entries from array that are not present in any of the other arrays.

Examples

Example #1 array_diff_uassoc() example

The "a" => "green" pair is present in both arrays and thus it is not in the output from the function. Unlike this, the pair 0 => "red" is in the output because in the second argument "red" has key which is 1 .

<?php
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>

The above example will output:

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

The equality of 2 indices is checked by the user supplied callback function.

Notes

Note :

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_uassoc($array1[0], $array2[0], "key_compare_func"); .

See Also

  • array_diff() - Computes the difference of arrays
  • array_diff_assoc() - Computes the difference of arrays with additional index check
  • array_udiff() - Computes the difference of arrays by using a callback function for data comparison
  • 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_intersect() - Computes the intersection of arrays
  • array_intersect_assoc() - Computes the intersection of arrays with additional index check
  • array_uintersect() - Computes the intersection of arrays, compares data by a callback function
  • 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
Read article

PHP / array_diff_ukey — DevDocs

array_diff_ukey

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

array_diff_ukey Computes the difference of arrays using a callback function on the keys for comparison

Description

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

Compares the keys from array against the keys from arrays and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

Unlike array_diff_key() a user supplied callback function is used for the indices comparison, not internal function.

Parameters

array

The array to compare from

arrays

Arrays to compare 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 an array containing all the entries from array that are not present in any of the other arrays.

Examples

Example #1 array_diff_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_diff_ukey($array1, $array2, 'key_compare_func'));
?>

The above example will output:

array(2) {
  ["red"]=>
  int(2)
  ["purple"]=>
  int(4)
}

Notes

Note :

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff_ukey($array1[0], $array2[0], 'callback_func'); .

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_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
  • array_intersect_ukey() - Computes the intersection of arrays using a callback function on the keys for comparison
Read article

PHP / array_fill — DevDocs

array_fill

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

array_fill Fill an array with values

Description

array_fill(int $start_index, int $count, mixed $value): array

Fills an array with count entries of the value of the value parameter, keys starting at the start_index parameter.

Parameters

start_index

The first index of the returned array.

If start_index is negative, the first index of the returned array will be start_index and the following indices will start from zero prior to PHP 8.0.0; as of PHP 8.0.0, negative keys are incremented normally (see example).

count

Number of elements to insert. Must be greater than or equal to zero, and less than or equal to 2147483647 .

value

Value to use for filling

Return Values

Returns the filled array

Errors/Exceptions

Throws a ValueError if count is out of range.

Changelog

Version Description
8.0.0 array_fill() now throws a ValueError if count is out of range; previously E_WARNING was raised, and the function returned false .

Examples

Example #1 array_fill() example

<?php
$a = array_fill(5, 6, 'banana');
print_r($a);
?>

The above example will output:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)

Example #2 array_fill() example with a negative start index

<?php
$a = array_fill(-2, 4, 'pear');
print_r($a);
?>

Output of the above example in PHP 7:

Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)

Output of the above example in PHP 8:

Array
(
    [-2] => pear
    [-1] => pear
    [0] => pear
    [1] => pear
)

Note that index -1 is not present prior to PHP 8.0.0.

Notes

See also the Arrays section of manual for a detailed explanation of negative keys.

See Also

  • array_fill_keys() - Fill an array with values, specifying keys
  • str_repeat() - Repeat a string
  • range() - Create an array containing a range of elements
Read article