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_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
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
PHP / array_fill_keys — DevDocs

array_fill_keys

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

array_fill_keys Fill an array with values, specifying keys

Description

array_fill_keys(array $keys, mixed $value): array

Fills an array with the value of the value parameter, using the values of the keys array as keys.

Parameters

keys

Array of values that will be used as keys. Illegal values for key will be converted to string .

value

Value to use for filling

Return Values

Returns the filled array

Examples

Example #1 array_fill_keys() example

<?php
$keys = array('foo', 5, 10, 'bar');
$a = array_fill_keys($keys, 'banana');
print_r($a);
?>

The above example will output:

Array
(
    [foo] => banana
    [5] => banana
    [10] => banana
    [bar] => banana
)

See Also

  • array_fill() - Fill an array with values
  • array_combine() - Creates an array by using one array for keys and another for its values
Read article
PHP / array_filter — DevDocs

array_filter

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

array_filter Filters elements of an array using a callback function

Description

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

Iterates over each value in the array passing them to the callback function. If the callback function returns true , the current value from array is returned into the result array .

Array keys are preserved, and may result in gaps if the array was indexed. The result array can be reindexed using the array_values() function.

Parameters

array

The array to iterate over

callback

The callback function to use

If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case.

mode

Flag determining what arguments are sent to callback :

  • ARRAY_FILTER_USE_KEY - pass key as the only argument to callback instead of the value
  • ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value
Default is 0 which will pass value as the only argument to callback instead.

Return Values

Returns the filtered array.

Changelog

Version Description
8.0.0 callback is nullable now.
8.0.0 If callback expects a parameter to be passed by reference, this function will now emit an E_WARNING .

Examples

Example #1 array_filter() example

<?php
function odd($var)
{
    // returns whether the input integer is odd
    return $var & 1;
}

function even($var)
{
    // returns whether the input integer is even
    return !($var & 1);
}

$array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
$array2 = [6, 7, 8, 9, 10, 11, 12];

echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
print_r(array_filter($array2, "even"));
?>

The above example will output:

Odd :
Array
(
    [a] => 1
    [c] => 3
    [e] => 5
)
Even:
Array
(
    [0] => 6
    [2] => 8
    [4] => 10
    [6] => 12
)

Example #2 array_filter() without callback

<?php

$entry = [
    0 => 'foo',
    1 => false,
    2 => -1,
    3 => null,
    4 => '',
    5 => '0',
    6 => 0,
];

print_r(array_filter($entry));
?>

The above example will output:

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

Example #3 array_filter() with mode

<?php

$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];

var_dump(array_filter($arr, function($k) {
    return $k == 'b';
}, ARRAY_FILTER_USE_KEY));

var_dump(array_filter($arr, function($v, $k) {
    return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
?>

The above example will output:

array(1) {
  ["b"]=>
  int(2)
}
array(2) {
  ["b"]=>
  int(2)
  ["d"]=>
  int(4)
}

Notes

Caution

If the array is changed from the callback function (e.g. element added, deleted or unset) the behavior of this function is undefined.

See Also

  • array_intersect() - Computes the intersection of arrays
  • array_map() - Applies the callback to the elements of the given arrays
  • array_reduce() - Iteratively reduce the array to a single value using a callback function
  • array_walk() - Apply a user supplied function to every member of an array
Read article
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
Read article