array_diff_key() function use in PHP 8.4 With Example

The array_diff_key() function in PHP 8 compares arrays by their keys and returns the difference. It checks the keys of the first array against subsequent arrays and returns an array containing entries from the first array whose keys are not present in the other arrays. Here’s how to use it:

Syntax

array_diff_key(array $array1, array $array2, array …$arrays): array

Key Features in PHP 8

  1. Strict Type Checks: PHP 8 enforces stricter type handling. If non-array arguments are passed, a TypeError is thrown.
  2. No Deprecations: Works the same as in PHP 7 but with stricter error handling.
  3. Performance: Optimized for PHP 8’s engine.

Basic Example

$array1 = ['name' => 'Alice', 'age' => 30, 'city' => 'Paris'];
$array2 = ['age' => 25, 'country' => 'France'];
$result = array_diff_key($array1, $array2);
print_r($result);

Output:

Array
(
[name] => Alice
[city] => Paris
)
  • The keys name and city from $array1 are absent in $array2, so they are returned.

Edge Cases

  1. Multiple Arrays
$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['a' => 4];
$array3 = ['c' => 5];
$result = array_diff_key($array1, $array2, $array3);
print_r($result); // Output: ['b' => 2]
  1. Empty Arrays
$array1 = ['key' => 'value'];
$array2 = [];
$result = array_diff_key($array1, $array2);
print_r($result); // Output: ['key' => 'value']

PHP 8 Error Handling

PHP 8 throws a TypeError if non-array arguments are passed:

// This will throw a TypeError in PHP 8
$result = array_diff_key(['a' => 1], 'not_an_array');

Fix:

if (is_array($input)) {
$result = array_diff_key(['a' => 1], $input);
}

array_diff() function in php 8.4 with example

The array_diff() function in PHP 8.4 computes the difference between two or more arrays, comparing their values. It returns an array containing the values from the first array that are not present in any of the subsequent arrays.

This function remains unchanged in PHP 8.4, continuing to work as in previous versions.

Syntax

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

Parameters

  1. $array (required): The array to compare.
  2. $arrays (required): One or more arrays to compare against.

Return Value

  • Returns an array containing the values from the first array that are not present in any of the other arrays.
  • The keys are preserved from the first array.

Example 1: Basic Usage

<?php
$array1 = ["a" => "red", "b" => "green", "c" => "blue"];
$array2 = ["a" => "red", "d" => "yellow"];
$result = array_diff($array1, $array2);
print_r($result);
?>

Output:

Array
(
[b] => green
[c] => blue
)

Explanation: The values "green" and "blue" are in $array1 but not in $array2.

Example 2: Multiple Arrays

<?php
$array1 = ["red", "green", "blue"];
$array2 = ["green", "yellow"];
$array3 = ["blue", "purple"];
$result = array_diff($array1, $array2, $array3);
print_r($result);
?>

Output:

Array
(
[0] => red
)

Explanation: The value "red" is in $array1 but not in $array2 or $array3.

Notes

  1. Comparison is case-sensitive, meaning "Red" and "red" are treated as different values.
  2. The function only compares values, not keys.

PHP 8.4 Context

The array_diff() function remains unchanged in PHP 8.4. It retains its same functionality and behavior as in earlier versions, ensuring backward compatibility.

If you have specific use cases or concerns about array_diff() in PHP 8.4, let me know!

array_count_values() function in php 8.4

The array_count_values() function in PHP is used to count the number of occurrences of each value in an array. This function has been consistent across PHP versions, including PHP 8.4, with no new changes introduced in this version.

Syntax

array_count_values(array $array): array

Parameters

$array: The input array. Each value in the array is counted, and the keys of the resulting array are the unique values from the input.

Return Value

  • Returns an associative array where the keys are the unique values from the input array and the values are the count of occurrences of each key.

Example 1: Basic Usage

<?php
$input = ["apple", "banana", "apple", "orange", "banana", "apple"];
$result = array_count_values($input);
print_r($result);
?>

Output:

Array
(
[apple] => 3
[banana] => 2
[orange] => 1
)

Example 2: Using Integer Values

<?php
$numbers = [1, 2, 3, 1, 2, 1];
$result = array_count_values($numbers);
print_r($result);
?>

Output:

Array
(
[1] => 3
[2] => 2
[3] => 1
)

PHP 8.4 Context

As of PHP 8.4, there are no updates or new features for the array_count_values() function. Its behavior remains the same, ensuring backward compatibility.

array_combine() function in PHP 8.4

The array_combine() function in PHP allows you to create an associative array by using one array for keys and another for values. It is available in all PHP versions, including PHP 8.4, and there haven’t been any changes to this function specific to PHP 8.4.

Syntax

array_combine(array $keys, array $values): array|false

Parameters

  • $keys: An array of keys to use for the new associative array.
  • $values: An array of values to associate with the keys.

Return Value

  • Returns the combined associative array where the keys come from $keys and the values come from $values.
  • Returns false if:
  • $keys and $values do not have the same number of elements.
  • Either $keys or $values is empty.

Example

// Two separate arrays for keys and values
$keys = ['name', 'age', 'city'];
$values = ['John', 30, 'New York'];
// Combine arrays into an associative array
$result = array_combine($keys, $values);
print_r($result);

Output:

Array
(
[name] => John
[age] => 30
[city] => New York
)

Notes

  1. If $keys contains duplicate values, only the last key-value pair will be retained in the result, as array keys must be unique.
  2. Both $keys and $values must be numerically indexed arrays of equal length; otherwise, the function returns false.

Error Handling Example

<?php 
$keys = ['name', 'age'];
$values = ['John']; // Fewer values than keys
$result = array_combine($keys, $values);
if ($result === false) {
echo "Error: The number of keys and values must be the same!";
}
?>

PHP 8.4 Context

As of PHP 8.4, the array_combine() function behavior and usage remain unchanged. You can rely on its consistent functionality.

array_column function in php 8.4

In PHP 8.4, the array_column() function continues to operate as it did in previous versions, without any changes or new features introduced. This function is used to extract values from a single column in a multi-dimensional array.

Syntax:

array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array

Parameters:

  • $array: The input array containing sub-arrays or objects from which to pull a column of values.
  • $column_key: The key of the column to retrieve values from. This can be an integer key, a string key name, or null. If set to null, the function will return complete arrays or objects.
  • $index_key (optional): The key to use as the index/keys for the returned array. This can be an integer key, a string key name, or null. If not provided or set to null, the function will use the default integer indexing.

Example:

// Sample array representing a record set
$records = [
[
'id' => 5698,
'first_name' => 'Peter',
'last_name' => 'Griffin',
],
[
'id' => 4767,
'first_name' => 'Ben',
'last_name' => 'Smith',
],
[
'id' => 3809,
'first_name' => 'Joe',
'last_name' => 'Doe',
],
];
// Extract the 'last_name' column
$lastNames = array_column($records, 'last_name');
print_r($lastNames);

Output:

Array
(
[0] => Griffin
[1] => Smith
[2] => Doe
)

In this example, array_column() retrieves the ‘last_name’ values from each sub-array within $records and returns them in a new array.

For more detailed information, you can refer to the official PHP documentation on array_column().