Working with Associative Arrays in PHP
The following is a list of PHP functions for manipulating associative arrays, including examples and links to the official PHP documentation for each function. These functions offer various ways to interact with associative arrays, from retrieving and merging data to applying transformations and filters.
-
Description: Checks if a specific key exists in an array.
-
Return type:
bool
-
Return value:
true
if the key exists,false
otherwiseExample: $user = ['name' => 'John','age' => 30,'email' => 'john@example.com'];if (array_key_exists('email', $user)) {echo 'Email exists.';}php
-
Description: Returns all the keys of an associative array.
-
Return Type:
array
-
Return Value: An array of keys from the original array
Example: $user = ['name' => 'John','age' => 30,'email' => 'john@example.com'];$keys = array_keys($user);print_r($keys); // Outputs: Array ( [0] => name [1] => age [2] => email )php
-
Description: Returns all the values from an associative array.
-
Return Type:
array
-
Return Value: An array of values from the original array
Example: $user = ['name' => 'John','age' => 30,'email' => 'john@example.com'];$values = array_values($user);print_r($values); // Outputs: Array ( [0] => John [1] => 30 [2] => john@example.com )php
-
Description: Merges one or more arrays.
-
Return Type:
array
-
Return Value: A new array containing elements from all input arrays
Example: $user = ['name' => 'John','age' => 30];$contact = ['email' => 'john@example.com','phone' => '123-456-7890'];$profile = array_merge($user, $contact);print_r($profile);// Outputs: Array ( [name] => John [age] => 30 [email] => john@example.com [phone] => 123-456-7890 )php
-
Description: Computes the difference of arrays with additional index check.
-
Return Type:
array
-
Return Value: An array containing all the values from the first array that are not present in the other arrays
Example: $user1 = ['name' => 'John','age' => 30,'email' => 'john@example.com'];$user2 = ['name' => 'John','age' => 31,'email' => 'john@example.com'];$diff = array_diff_assoc($user1, $user2);print_r($diff); // Outputs: Array ( [age] => 30 )php
-
Description: Creates an array by using one array for keys and another for its values.
-
Return Type:
array
-
Return Value: An associative array with keys from the first array and values from the second array
Example: $keys = ['name', 'age', 'email'];$values = ['John', 30, 'john@example.com'];$user = array_combine($keys, $values);print_r($user);// Outputs: Array ( [name] => John [age] => 30 [email] => john@example.com )php
-
Description: Filters elements of an array using a callback function.
-
Return Type:
array
-
Return Value: An array containing all the elements of the input array that pass the test implemented by the callback function
Example: $users = [['name' => 'John', 'age' => 30],['name' => 'Jane', 'age' => 25],['name' => 'Doe', 'age' => 35]];$adults = array_filter($users, function($user) {return $user['age'] >= 30;});print_r($adults);// Outputs: Array ( [0] => Array ( [name] => John [age] => 30 ) [2] => Array ( [name] => Doe [age] => 35 ) )php
-
Description: Applies a callback to the elements of the given arrays.
-
Return Type:
array
-
Return Value: An array containing the results of applying the callback function to each element
Example: $users = [['name' => 'John', 'age' => 30],['name' => 'Jane', 'age' => 25],['name' => 'Doe', 'age' => 35]];$userNames = array_map(function($user) {return $user['name'];}, $users);print_r($userNames);// Outputs: Array ( [0] => John [1] => Jane [2] => Doe )php
-
Description: Applies a user-defined function to each element of an array.
-
Return Type:
bool
-
Return Value:
true
on success,false
on failureExample: $users = [['name' => 'John', 'age' => 30],['name' => 'Jane', 'age' => 25]];array_walk($users, function($user) {$user['email'] = strtolower($user['name']) . '@example.com';});print_r($users);// Outputs: Array ( [0] => Array ( [name] => John [age] => 30 [email] => john@example.com ) [1] => Array ( [name] => Jane [age] => 25 [email] => jane@example.com ) )php
-
Description: Iteratively reduces the array to a single value using a callback function.
-
Return Type:
mixed
-
Return Value: The final reduced value, which is the result of the callback function applied across the array
Example: $users = [['name' => 'John', 'age' => 30],['name' => 'Jane', 'age' => 25],['name' => 'Doe', 'age' => 35]];$totalAge = array_reduce($users, function($carry, $user) {return $carry + $user['age'];}, 0);echo $totalAge;// Outputs: 90php