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_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
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
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