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

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

PHP / array_is_list — DevDocs

array_is_list

(PHP 8 >= 8.1.0)

array_is_list Checks whether a given array is a list

Description

array_is_list(array $array): bool

Determines if the given array is a list. An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1 .

Parameters

array

The array being evaluated.

Return Values

Returns true if array is a list, false otherwise.

Examples

Example #1 array_is_list() example

<?php

array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true

// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false

// The keys are not in the correct order
array_is_list([1 => 'apple', 0 => 'orange']); // false

// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false

// Non-consecutive keys
array_is_list([0 => 'apple', 2 => 'bar']); // false
?>

Notes

Note :

This function returns true on empty arrays.

See Also

  • array_values() - Return all the values of an array
Read article

PHP / array_key_exists — DevDocs

array_key_exists

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

array_key_exists Checks if the given key or index exists in the array

Description

array_key_exists(string|int $key, array $array): bool

array_key_exists() returns true if the given key is set in the array. key can be any value possible for an array index.

Parameters

key

Value to check.

array

An array with keys to check.

Return Values

Returns true on success or false on failure.

Note :

array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.

Examples

Example #1 array_key_exists() example

<?php
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
    echo "The 'first' element is in the array";
}
?>

Example #2 array_key_exists() vs isset()

isset() does not return true for array keys that correspond to a null value, while array_key_exists() does.

<?php
$search_array = array('first' => null, 'second' => 4);

// returns false
isset($search_array['first']);

// returns true
array_key_exists('first', $search_array);
?>

Notes

Note :

For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array . This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.

To check whether a property exists in an object, property_exists() should be used.

See Also

  • isset() - Determine if a variable is declared and is different than null
  • array_keys() - Return all the keys or a subset of the keys of an array
  • in_array() - Checks if a value exists in an array
  • property_exists() - Checks if the object or class has a property
Read article

PHP / array_key_first — DevDocs

array_key_first

(PHP 7 >= 7.3.0, PHP 8)

array_key_first Gets the first key of an array

Description

array_key_first(array $array): int|string|null

Get the first key of the given array without affecting the internal array pointer.

Parameters

array

An array.

Return Values

Returns the first key of array if the array is not empty; null otherwise.

Examples

Example #1 Basic array_key_first() Usage

<?php
$array = ['a' => 1, 'b' => 2, 'c' => 3];

$firstKey = array_key_first($array);

var_dump($firstKey);
?>

The above example will output:

string(1) "a"

Notes

Tip

There are several ways to provide this functionality for versions prior to PHP 7.3.0. It is possible to use array_keys() , but that may be rather inefficient. It is also possible to use reset() and key() , but that may change the internal array pointer. An efficient solution, which does not change the internal array pointer, written as polyfill:

<?php
if (!function_exists('array_key_first')) {
    function array_key_first(array $arr) {
        foreach($arr as $key => $unused) {
            return $key;
        }
        return NULL;
    }
}
?>

See Also

  • array_key_last() - Gets the last key of an array
  • reset() - Set the internal pointer of an array to its first element
Read article

PHP / array_key_last — DevDocs

array_key_last

(PHP 7 >= 7.3.0, PHP 8)

array_key_last Gets the last key of an array

Description

array_key_last(array $array): int|string|null

Get the last key of the given array without affecting the internal array pointer.

Parameters

array

An array.

Return Values

Returns the last key of array if the array is not empty; null otherwise.

See Also

  • array_key_first() - Gets the first key of an array
  • end() - Set the internal pointer of an array to its last element
Read article