PHP Functions (Arrays) (Best Tutorial 2019)

PHP Functions

PHP Functions—Comparing and Merging Arrays

In this blog we will take a brief look at the PHP functions that compare and merge arrays. Many of these functions work with multiple array types (single, multidimensional, and/or associative).

 

All descriptions of the following functions are provided by the online PHP manual available at www.php.net. Brief examples using each function and a description of the results of these examples are provided.

 

These examples are only meant to get your feet wet. For a more detailed description of the functions shown, and for more examples, visit the online PHP manual.

 

Comparing Arrays

array_diff_assoc computes the difference of two associative arrays with additional index check “Compares array1 against array2 and returns the difference.”

Syntax: array array_diff_assoc ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001);
$difference = array_diff_assoc($first_array, $second_array); print_r($difference);
Output:
Array
(
[first_name] => Steve
[last_name] => Smith
[address] => 123 Main Street
)

 

This example compares $first_array and $second_array. It returns was is different about the first array. If we switch the parameters

Example:

$difference = array_diff_assoc($second_array, $first_array);
Output:
Array
(
[first_name] => Thesis
[last_name] => Scientist
[address] => 101 South Street
)

 

This example compares $first_array and $second_array. It returns was is different about the second array. array_diff_key

 

computes the difference of arrays using keys for comparison “Compares the keys from array1 against the keys from array2 and returns the difference.”

Syntax: array array_diff_key ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
var_dump(array_diff_key($first_array, $second_array));
Output:
array(3)
{
["first_name"]=> string(4) "Steve"
["last_name"]=> string(5) "Smith"
["zip"]=> int(30001)
}

 

This example compares the keys in $first_array and $second_array. If the keys are different, it returns an array with the keys that are different in the first array. If we switch parameters

Example:

var_dump(array_diff_key($second_array, $first_array));

Output:

array(3)

{

["firstname"]=> string(5) "Thesis"

["lastname"]=> string(6) "Scientist"

["zipcode"]=> int(30001)

}

This example compares the keys in $first_array and $second_array. If the keys are different, it returns an array with the keys that are different in the second array.

 

array_diff_uassoc

array_diff_uassoc

computes the difference of arrays with additional index check which is performed by a user-supplied callback function “Compares array1 against array2 and returns the difference.”

 

Syntax: array array_diff_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001);
function key_compare($first_value, $second_value)
{
if ($first_value === $second_value) {
return 0;
//
if they are the same do return a value
}
return ($first_value > $second_value)? 1:-1;
//
if the first value is greater, return that value.
}
print_r( array_diff_uassoc($first_array,
$second_array, "key_compare"));
Output:
Array (
[first_name] => Steve
[last_name] => Smith
[address] => 123 Main Street
)

In this example, the function key_compare causes the values in the first array ($first_array) to be returned.

Example:

print_r( array_diff_uassoc($second_array, $first_array, "key_compare"));
Output:
Array (
[first_name] => Thesis
[last_name] => Scientist
[address] => 101 South Street
)

In this example, the function key_compare causes the values in the first array ($second_array) to be returned.

 

array_diff_ukey

computes the difference of arrays using a callback function on the keys for comparison “Compares the keys from array1 against the keys from array2 and returns the difference.”

 

Syntax: array array_diff_ukey ( array $array1 , array $array2 [, array $... ], callable $key_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
function key_compare($first_value, $second_value)
{
if ($first_value == $second_value) {
return 0;
}
return ($first_value > $second_value)? 1:-1;
}
var_dump(array_diff_ukey($first_array,
$second_array, key_compare));
Output:
array(3) {
["first_name"]=> string(4) "Steve"
["last_name"]=> string(5) "Smith"
["zip_code"]=> int(30001)
}

 

In this example, the function key_compare compares the keys and returns the key and value from the first array ($first_array), when there is a difference.

Example:

var_dump(array_diff_ukey($second_array, $first_array, 'key_compare'));
Output:
array(3) {
["firstname"]=> string(5) "Thesis"
["lastname"]=> string(6) "Scientist"
["zipcode"]=> int(30001)
}

 

In this example, the function key_compare compares the keys and returns the key and value from the first array ($second_array), when there is a difference. 

 

PHP compare function can be used instead of a user-defined function. These could include strcmp, strcasecmp, strncasecmp, substrcomp, or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_diff

array_diff

computes the difference of arrays “Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.”

Syntax: array array_diff ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array("a", “b”, “c”, “d”, “e”);

$second_array = array("a”, “d”, “f”, “g”, “h”);

$difference = array_diff($first_array, $second_array);

print_r($difference);

 

In this example, the arrays are compared. If a difference is found, the key and value from the first array ($first_array) is returned.

 

array_udiff_assoc

computes the difference of arrays with additional index check, compares data by a callback function array_udiff_assoc() returns an array containing all the values from array1 that are not present in any of the other arguments.

 

Note that the keys are used in the comparison unlike array_diff() and array_udiff(). The comparison of arrays' data is performed by using a user-supplied callback.”

 

Syntax: array array_udiff_assoc ( array $array1 , array $array2 [, array $... ], callable $value_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
function key_compare($first_key, $second_key)
{
if ($first_key == $second_key)
return 0;
else if ($first_key > $second_key)
return 1;
else
return -1;
}
print_r(array_udiff_assoc($first_array,
$second_array, "key_compare"));
Output:
Array (
[first_name] => Steve
[last_name] => Smith
[state] => CA
[zip_code] => 30001
)

 

In this example, $first_array includes a different index (key) for first name than $second_array. It also includes a different value in last_name and state. The ZIP code index (key) is also different.

 

The user-supplied method causes the indexes (keys) and values from $first_array to be returned when there is a difference in either or both the index (key) and value. 

 

A PHP compare function can be used instead of a user-defined function. These could include strcmp, strcasecmp, strncasecmp, substrcomp, or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_udiff_uassoc

array_udiff_uassoc

computes the difference of arrays with additional index check, compares data and indexes by a callback function “Returns an array containing all the values from array1 that are not present in any of the other arguments.”

 

Syntax: array array_udiff_uassoc ( array $array1 , array $array2 [, array $... ], callable $value_ compare_func , callable $key_compare_func )

 

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
function key_compare($first_key, $second_key)
{
if ($first_key == $second_key)
return 0;
else if ($first_key > $second_key)
return 1;
else
return -1;
}
function value_compare($first_value,
$second_value)
{
if ($first_value == $second_value)
return 0;
else if ($first_value > $second_value)
return 1;
else
return -1;
}
print_r(array_udiff_uassoc($first_array,
$second_array, "value_compare", "key_compare"));
Output:
Array (
[first_name] => Steve
[last_name] => Smith
[state] => CA
[zip_code] => 30001
)

 

The user must supply a function for comparing both the indexes and the values. In this example, both methods use the same logic to make a comparison. If the keys are different, the first_array key and value are displayed. If the values are different, the first_array key and value are also displayed.

 

Thus, first_name is displayed because the indexes are different. last_name is displayed because the values are different. the state is displayed because the values are different. zip_code is displayed because the indexes are different.

 

A PHP compare function can be used instead of a user-defined function. These could include strcmp, strcasecmp, strncasecmp, substrcomp, or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_udiff

computes the difference of arrays by using a callback function for data comparison “Returns an array containing all the values of array1 that are not present in any of the other arguments.”

 

Syntax: array array_udiff ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
function value_compare($first_value, $second_value)
{
if ($first_value == $second_value)
return 0;
else if ($first_value > $second_value)
return 1;
else
return -1;
}
print_r(array_udiff ($first_array,
$second_array, "value_compare"));
Output:
Array (
[last_name] => Smith
[state] => CA
)

 

The user has to supply a function that will compare the values. In this example, if the values are different, the key and value from $first_array are displayed. Index differences are ignored. The values in last_name and state are different in the two arrays.

 

 PHP compare function can be used instead of a user-defined function. These could include strcmp, strcasecmp, strncasecmp, substrcomp, or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_uintersect_assoc

array_uintersect_assoc

computes the intersection of arrays with additional index check, compares data by a callback function Returns an array containing all the values of array1 that are present in all the arguments.”

 

Syntax: array array_uintersect_assoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
print_r(array_uintersect_assoc($first_array,
$second_array, "strcasecmp"));
Output:
Array (
[address] => 123 Main Street
[city] => Paris
)

 

This example is using the PHP method strcasecmp to compare strings ignoring the case. If the strings (and the keys) match, the key and index are returned. Only address and city are matched in both arrays. The programmer can supply a function as shown in other examples. The supplied function must return results <0, 0, and > 0.

 

Other possible PHP functions that can be used include strcmp, strncasecmp, substrcomp, or any other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_uintersect_uassoc

computes the intersection of arrays with additional index check, compares data and indexes by separate callback functions “Returns an array containing all the values of array1 that are present in all the arguments.”

 

Syntax:

array array_uintersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $value_compare_func , callable $key_compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
function key_compare($first_key, $second_key)
{
if ($first_key == $second_key)
return 0;
else if ($first_key > $second_key)
return 1;
else
return -1;
}
function value_compare($first_value, $second_value)
{
if ($first_value == $second_value)
return 0;
else if ($first_value > $second_value)
return 1;
else
return -1;
}
print_r(array_uintersect_uassoc($first_array,
$second_array, "key_compare", "value_compare"));
Output:
Array (
[address] => 123 Main Street
[city] => Paris
)

This function requires both key and value user-supplied functions. PHP functions could be used (such as strcascmp).

 

The user-supplied functions return the key and value if both the key and value are exact matches (except for the case due to == instead of ===). In this example, the address and city are the only matches (for both key and value).

 

A PHP compare function can be used instead of a user-defined function. These could include strcmp, strcasecmp, strncasecmp, substrcomp, or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_uintersect

array_uintersect

computes the intersection of arrays, compares data by a callback function “Returns an array containing all the values of array1 that are present in all the arguments. “

 

Syntax: array array_uintersect ( array $array1 , array $array2 [, array $... ], callable $value_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Steve' ,
'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zipcode' => 30001);
function value_compare($first_value, $second_value)
{
if ($first_value == $second_value)
return 0;
else if ($first_value > $second_value)
return 1;
else
return -1;
}
print_r(array_uintersect($first_array,
$second_array, "value_compare"));
Output:
Array (
[first_name] => Steve
[address] => 123 Main Street
[city] => Paris
[zip_code] => 30001
)

This function will ignore differences in the key (subscript). It uses the user-supplied function to compare values. If the values match, the key and value from the first array ($first_array) are returned. 

 

In this example, first name, address, city, and ZIP code are returned because they exist in both arrays, even though the keys (subscripts) vary in each array.

 

The user can modify the comparison shown (such as using === instead of ==) to determine what is “equal.” The function must return a value < 0, 0, and > 0.

 

print_r(array_uintersect($first_array, $second_array, "strcmp”));

 

A PHP compare function can be used instead of a user-defined function. 
These could include strcmp, strcasecmp, strncasecmp, substrcomp, 
or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

Merging Arrays

Merging Arrays

array_combine

creates an array by using one array for keys and another for its value “Creates an array using the values from the keys array as keys and the values from the values array as the corresponding values.

Syntax: array array_combine ( array $keys , array $values )

Example:

$first_names = array('Steve', 'Thesis', 'Fred');
$last_names = array('Smith', 'Scientist', 'Jones');
$names = array_combine($last_names,
$first_names);
print_r($names);
Output:
Array
(
[Smith] => Steve
[Scientist] => Thesis
[Jones] => Fred
)

 

This example uses the $last_names array to populate the keys and the $first_names array to populate the values.

 

array_intersect_assoc

computes the intersection of arrays with additional index check “array_intersect_assoc() returns an array containing all the values of array1 that are present in all the arguments.

 

Note that the keys are used in the comparison unlike in array_intersect().”

This function compares arrays and returns what is common in both arrays. The index and the value must both be the same.

Syntax:

array array_intersect_assoc ( array $array1 , array $array2 [, array $... ] )

 

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
$result_array = array_intersect_assoc($first_array, $second_array);
print_r($result_array);
Output:
Array (
[city] => Paris
[state] => GA
)

Notice that ZIP code was not included because the indexes were not the same. Only city and state are the same in both arrays.

 

array_intersect_key

array_intersect_key

computes the intersection of arrays using keys for comparison “array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.”

Syntax: array array_intersect_key ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
$result_array = array_intersect_key($first_array, $second_array);
print_r($result_array);
Output:
Array (
[address] => 123 Main Street
[city] => Paris
[state] => GA
)

 

This function compares the keys (only) and returns any key/value pair from the first array that has a matching key in the second array. In this example, even though the values are different in each array, address, city, and state keys are the same. The values from the first array ($first_array) are also passed.

 

array_intersect_uassoc

computes the intersection of arrays with additional index check, compares indexes by a callback function “array_intersect_uassoc() returns an array containing all the values of array1 that are present in all the arguments. Note that the keys are used in the comparison unlike in array_intersect().”

 

Syntax: array array_intersect_uassoc ( array $array1 , array $array2 [, array $... ], callable $key_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
$result_array = array_intersect_uassoc($first_array, $second_array, "strcasecmp");
print_r($result_array);
Output:
Array (
[city] => Paris
[state] => GA
)

 

This example returns the common values Paris and GA but not the ZIP code since the indexes are different. strcasecmp is a PHP function that compares strings. If they are an exact match then this example would return the value as shown (from $first_array). Other PHP functions which return 0, >0, or <0 can be used (see next example).

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001); $second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
function key_compare($first_key, $second_key)
{
if ($first_key == $second_key)
return 0;
else if ($first_key > $second_key)
return 1;
else
return -1;
}
$result_array = array_intersect_uassoc($first_array, $second_array, "key_compare");
print_r($result_array);
Output:
Array (
[city] => Paris
[state] => GA
)

The programmer can create their own functions. However, they must return 0 (exact match),

< 0 (the first array is less than the second array), or >0 (the first array is greater than the second array).

Other PHP functions that can be used include strcmp, strcasecmp, substrcomp,

or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_intersect_ukey

array_intersect_ukey

computes the intersection of arrays using a callback function on the keys for comparison “array_intersect_ukey() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.”

 

Syntax: array array_intersect_ukey ( array $array1 , array $array2 [, array $... ], callable $key_ compare_func )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
function key_compare($first_key, $second_key)
{
if ($first_key == $second_key)
return 0;
else if ($first_key > $second_key)
return 1;
else
return -1;
}
$result_array = array_intersect_ukey($first_array, $second_array, 'key_compare');
print_r($result_array);
Output:
Array (
[address] => 123 Main Street
[city] => Paris
[state] => GA
)

This function ignores the values and only compares keys. Thus, in this example address is returned because the keys match even though the values are different. You can use PHP functions (such as strcasecmp) instead of your own functions.

 

PHP compare function can be used instead of a user-defined function.

These could include strcmp, strcasecmp, strncasecmp, substrcomp,

or other PHP comparison functions that return < 0, 0, or > 0 as a result.

 

array_intersect

computes the intersection of arrays “array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.”

 

Syntax: array array_intersect ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' ,
'lastname' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
$result_array = array_intersect($first_array,
$second_array);
print_r($result_array);
Output:
Array (
[city] => Paris
[state] => GA
[zip_code] => 30001
)

This function compares values and ignores keys. It returns the key that exists in the first array.

 

array_merge_recursive

merge two or more arrays recursively “array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

 

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too.

 

If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.”

 

Syntax: array array_merge_recursive ( array $array1 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001);
$result = array_merge_recursive($first_array,
$second_array);
print_r($result);
Output:
Array (
[first_name] => Array
( [0] => Steve
[1] => Thesis
)
[last_name] => Array
( [0] => Smith
[1] => Scientist
)
[address] => Array
(
[0]
=> 123 Main Street
[1]
=> 101 South Street
)
[city] => Array
( [0] => Paris
[1] => Paris
)
[state] => Array
( [0] => GA
[1] => GA
)
[zip_code] => Array
( [0] => 30001
[1] => 30001
)
)

Duplicate keys are placed into an array as shown above. This produces a multidimensional array with rows (arrays) of like items, such as first_name.

 

array_merge

array_merge

merge one or more arrays “Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

 

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.

 

If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.”

 

Syntax: array array_merge ( array $array1 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001);
$result = array_merge($first_array, $second_array); print_r($result);
Output:
Array (
[first_name] => Thesis
[last_name] => Scientist
[address] => 101 South Street
[city] => Paris
[state] => GA
[zip_code] => 30001
)

If the keys are the same in both arrays the second array will dominate as shown above.

 

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('firstname' => 'Thesis' , 'lastname' => 'Scientist' , 'add' => '101 South Street' , 'city' => 'Paris' , 'st' => 'GA' , 'zipcode' => 30001);
$result = array_merge($first_array, $second_array); print_r($result);
Output:
Array (
[first_name] => Steve
[last_name] => Smith
[address] => 123 Main Street
[city] => Paris
[state] => GA
[zip_code] => 30001
[firstname] => Thesis
[lastname] => Scientist
[add] => 101 South Street
[cty] => Paris
[st] => GA
[zipcode] => 30001
)

If the indexes are different, the second array will be appended to the first array as shown above.

 

Example:

$first_array = array('Steve' , 'Smith' , '123 Main Street' , 'Paris', 'GA', 30001); $second_array = array('Thesis' , 'Scientist' , '101 South Street' , 'Paris' ,'GA' , 30001);
$result = array_merge($first_array, $second_array); print_r($result);
Output:
Array (
[0]
=> Steve
[1]
=> Smith
[2]
=> 123 Main Street
[3]
=> Paris
[4]
=> GA
[5]
=> 30001
[6]
=> Thesis
[7]
=> Scientist
[8]
=> 101 South Street
[9]
=> Paris
[10]
=> GA
[11]
=> 30001
)

If the arrays are numeric, the keys will be renumbered for the new array created by merging $first_array and $second_array.

 

array_replace_recursive

array_replace_recursive

replaces elements from passed arrays into the first array recursively

“array_replace_recursive() replaces the values of array1 with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array.

 

If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.”

 

Syntax: array array_replace_recursive ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' , 'lastname' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
print_r(array_replace_recursive($first_array,
$second_array));
Output:
Array (
[first_name] => Thesis
[last_name] => Smith
[address] => 101 South Street
[city] => Paris
[state] => GA
[zip_code] => 30001
[lastname] => Scientist
[zipcode] => 30001
)

Comparing $first_array with $second_array, the last name and ZIP code indexes are different. This function will attempt to substitute the values contained in the array in the second parameter

 

($second_array) into the array in the first parameter ($first_array). If there is not a related value in the second array (there is no last_name or zip_code), it will retain the values from the first array.

 

If there are values in the second array that are not in the first array, it will add them to the end of the array (last name, zip code) produced. This process is recursive.

 

array_replace

replaces elements from passed arrays into the first array “array_replace() replaces the values of array1 with values having the same keys in each of the following arrays.

 

If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array.

 

If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values. “

 

Syntax: array array_replace ( array $array1 , array $array2 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
$second_array = array('first_name' => 'Thesis' , 'lastname' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zipcode' => 30001);
print_r(array_replace($first_array, $second_array));
Output:
Array (
[first_name] => Thesis
[last_name] => Smith
[address] => 101 South Street
[city] => Paris
[state] => GA
[zip_code] => 30001
[lastname] => Scientist
[zipcode] => 30001
)

Comparing $first_array with $second_array, the last name and ZIP code indexes are different. This function will attempt to substitute the values contained in the array in the second parameter ($second_ array) into the array in the first parameter ($first_array).

 

If there is not a related value in the second array (there is no last_name or zip_code), it will retain the values from the first array.

 

If there are values in the second array that are not in the first array, it will add them to the end of the array (last name, zip code) produced. This process is not recursive.

 

PHP Functions—Searching, Traversing, and Displaying Arrays

PHP Functions

In this final blog, we will take a brief look at the PHP functions to search, traverse, and display arrays. Many of these functions work with multiple array types (single, multidimensional, and/or associative).

 

All descriptions of the following functions are provided by the online PHP manual available at www.php.net.

 

Brief examples using each function and a description of the results of these examples are provided. These examples are only meant to get your feet wet. For a more detailed description of the functions shown, and for more examples, visit the online PHP manual.

 

Searching Arrays

array_count_values counts all the values of an array “Returns an array using the values of the array as keys and their frequency in the array as values.”

Syntax: array array_count_values ( array $array )

Example:

$silly_array = array("hey", "hey", "hey", "what", "is", “this”, “unsure”, “unsure”);
print_r(array_count_values($silly_array));
Output:
Array
(
[hey] => 3
[what] => 1
[is] => 1
[this] => 1
[unsure] => 2
)

 

This example counts the frequency of the values in $silly_array. It creates an array that uses the value as an index and the frequency as the value. array_column returns the values from a single column in the input array.

 

“array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.”

 

Syntax: array array_column ( array $input , mixed $column_key [, mixed $index_key = null ] )

Example:

$customer_record = array (
array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001), array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' , 'address' => '101 South Street' , 'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001)
);
$first_names = array_column($customer_record,
'first_name');
print_r($first_names);
Output:
Array
(
[0]
=> Steve
[1]
=> Thesis
)

In this example, all first names in the customer_array are returned. In PHP 7 you can also specify an index in the second parameter.

 

Example:

$customer_record = array (
array('first_name' => 'Steve' ,
'last_name' => 'Smith' ,
'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA',
'zip_code' => 30001),
array('first_name' => 'Thesis' ,
'last_name' => 'Scientist' ,
'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' ,
'zip_code' => 30001)
);
$first_names = array_column($customer_record,
'first_name', ‘last_name’);
print_r($first_names);
Output:
Array
(
[Smith] => Steve
[Scientist] => Thesis
)

In this example, the values in last_name are used to populate the new keys (subscripts) which are returned from the function.

 

array_key_exists

checks if the given key or index exists in the array “array_key_exists() returns TRUE if the given key is set in the array. Key can be any value possible for an array index.”

 

Syntax: bool array_key_exists ( mixed $key , array $array )

Example:

$first_array = array('first_name' => 'Steve' ,

'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);

if (array_key_exists('first_name', $first_array)) {

print "The 'first_name' is in the array";

}

Output: The ‘first_name’ is in the array. This function eliminates the need to create a loop when searching for a key in an array.

 

array_keys

array_keys

returns all the keys or a subset of the keys of an array “array_keys() returns the keys, numeric and string, from the array.”

Syntax: array array_keys ( array $array [, mixed $search_value = null [, bool $strict = false ]] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
print_r(array_keys($first_array));
Output:
Array (
[0]
=> first_name
[1]
=> last_name
[2]
=> address
[3]
=> city
[4]
=> state
[5]
=> zip_code
)
This example returns all keys that exist in array $first_name.

 

array_search

searches the array for a given value and returns the corresponding key if successful “Searches haystack for a needle.”

Syntax: mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )

Example:

$first_array = array('first_name' => 'Steve' ,

'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);

print array_search('123 Main Street', $first_array);

 

Output: address

The value passed has to be an exact match. Passing ‘Main’ only in this example would not return a match.

 

in_array

checks if a value exists in an array

“Returns TRUE if the needle is found in the array, FALSE otherwise.”

 

Syntax: bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
if(in_array("Steve",$first_array))
{
print "Found Steve!";
}
else
{
print "WE LOST STEVE!";
}

Output: Found Steve!

in_array returns TRUE if the item searched is in the array. It does not return the position of the item in the array. In this example, Steve is found in the array.

key_exists

alias of array_key_exists()

“key_exists — Checks if the given key or index exists in the array”

 

Syntax: bool key_exists ( mixed $key , array $array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
if (key_exists('first_name', $first_array)) {
print "The 'first_name' is in the array";
}

Output:

The Main ‘first_name’ is in the array

This function eliminates the need to create a loop when searching for a key in an array.

 

Traversing Arrays

Traversing Arrays

current

returns the current element in an array “The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way.

 

If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE.”

 

Syntax: mixed current ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print current($first_array) . "<br>";
print next($first_array) . "<br>";
print prev($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

current works with next, previous, and end to display values located in positions in an array. currently does not actually move the pointer (position currently accessed) in the array. As shown in this example, only the values are displayed not the keys (subscripts).

 

each

returns the current key and value pair from an array and advances the array cursor “After each() has executed, the array cursor will be left on the next element of the array, or past the last element if it hits the end of the array. You have to use reset() if you want to traverse the array again using each.”

 

Syntax: array each ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print_r(each($first_array));
print_r(each($first_array));
Output:
Array (
[1]
=> Steve
[value] => Steve
[0]
=> first_name [key] => first_name
)
Array (
[1]
=> Smith
[value] => Smith
[0]
=> last_name [key] => last_name
)

each will place the key from the current position in two different elements ([0] and [key]). It will also place the value in two different elements ([1] and [value]). It will also move the cursor (location in the array) to the next element. In this example, the key and value are pulled from the first position of the $first_name array.

 

Then the cursor is moved to the next element. The function is called again displaying the information from the second element. The cursor is now positioned at the third element.

 

end

sets the internal pointer of an array to its last element “end() advances array's internal pointer to the last element, and returns its value.”

Syntax: mixed end ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print current($first_array) . "<br>";
print next($first_array) . "<br>";
print prev($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

In this example, end moves the cursor to the last element and returns the value in the element. The ZIP code is displayed.

 

key

key-array

fetches a key from an array “The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way.”

Syntax: mixed key ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,

'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);

print key($first_array);

 

Output: first_name

In this example, the array is currently at the top. the key will display the key (subscript) at the current position. This function will not move the cursor.

 

next

advances the internal array pointer of an array “next() behaves like current(), with one difference. It advances the internal array pointer one place forward before returning the element value. That means it returns the next array value and advances the internal array pointer by one.”

 

Syntax: mixed next ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print current($first_array) . "<br>";
print next($first_array) . "<br>";
print prev($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

Next moves to the next element and displays it. In this example, current displays the first value (Steve) but does not advance to the next element. next advances to the next element and displays it (Smith).

 

pos

alias of current() “The pos() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way.

 

If the internal pointer points beyond the end of the elements list or the array is empty, pos() returns FALSE.”

 

Syntax: mixed pos ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print pos($first_array) . "<br>";
print next($first_array) . "<br>";
print prev($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

pos displays the current value but does not move to the next value. In this example, pos will display Steve from the first position but will not move the cursor to the next position. next moves to the next position and displays the value (Smith).

 

prev

prev

rewinds the internal array pointer

“prev() behaves just like next(), except it rewinds the internal array pointer one place instead of advancing it.”

Syntax: mixed prev ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print pos($first_array) . "<br>";
print next($first_array) . "<br>";
print prev($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

In this example, pos displays the first value (Steve). next moves to the next value and displays it (Smith).

Prev moves back a position (back to the first position) and displays the value (Steve).

 

reset

sets the internal pointer of an array to its first element “reset() rewinds array's internal pointer to the first element and returns the value of the first array element.”

 

Syntax: mixed reset ( array &$array )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print pos($first_array) . "<br>";
print next($first_array) . "<br>";
print reset($first_array) . "<br>";
print end($first_array) . "<br>";
Output:
Steve
Smith
Steve
30001

reset moves the pointer to the top of the array. In this example, pos returns value in the first element but does not move the pointer. Next move the pointer to the second element and then returns the value stored in it. 

 

reset moves back to the top of the array and returns the value in the first element. the end is the reverse of reset; it moves to the last position of the array and returns the value in that position.

 

Displaying Array Contents

array_map

array_map

applies the callback to the elements of the given arrays

“array_map() returns an array containing all the elements of array1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()”

 

Syntax: array array_map ( callable $callback , array $array1 [, array $... ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001);
function add_info($value)
{
return("This value is " . $value);
}
print_r(array_map("add_info", $first_array));
Output:
Array (
[first_name] => This value is Steve
[last_name] => This value is Smith
[address] => This value is 123 Main Street
[city] => This value is Paris
[state] =>This value is GA
[zip_code] => This value is 30001
)

The function will apply changes to each element, as shown in the function passed (add_info), to the array passed to produce a new array with the keys intact. In this example This value is appended to the value of each position in the array to produce a new array.

 

array_product

calculates the product of values in an array “array_product() returns the product of values in an array.”

Syntax: number array_product ( array $array )

Example:

$product = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); print array_product($product);
Output:
3628800
In this example will multiply:
1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x 10 =
362880

 

array_rand

Pick one or more random entries out of an array “Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.”

 

Syntax:mixed array_rand ( array $array [, int $num = 1 ] )

Example:

$first_array = array('Steve' , 'Smith' ,
'123 Main Street' , 'Paris', 'GA', '30001');
print_r (array_rand($first_array, 2));
Output:
Array (
[0]
=> 3
[1]
=> 5
)

In this example array_rand picks the third element (Paris) and the fifth element (30001) because two elements were requested.

 

array_reduce

array_reduce

iteratively reduces the array to a single value using a callback function “array_reduce() applies iteratively the callback function to the elements of the array, so as to reduce the array to a single value.”

 

Syntax: mixed array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] )

Example:

$numerical_values =
array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
function add($value, $number)
{
$value += $number;
return $value;
}
function subtract($value, $number)
{
$value -= $number;
return $value;
}
function multiply($value, $number)
{
$value *= $number;
return $value;
}
function divide($value, $number)
{
$value /= $number;
return $value;
}
print array_reduce($numerical_values,
"add") . "<br>";
print array_reduce($numerical_values,
"subtract") . "<br>";
print array_reduce($numerical_values, "multiply", -5) . "<br>";; // start with a $value of -5 print array_reduce($numerical_values,
"divide", 5) . "<br>"; //start with a $value of 5
Output:
55
-55
-18144000
1.3778659611993E-6

 

array_reduce allows you to create your own function to traverse through an array, pick each item, and do something with the item. In these examples, functions are shown to add, subtract, multiply, and divide the items in the array.

print array_reduce($numerical_values, "multiply", -5) . "<br>";; // start with a $value of -5

 

This call starts with an initial value of –5 (which is placed in $value). The result (as seen in the preceding) is a negative value.

print array_reduce($numerical_values,

"divide", 5) . "<br>"; //start with a $value of 5

This call starts with the 5 being placed into $value before each element value is divided.

 

array_sum

calculates the sum of values in an array “Returns the sum of values as an integer or float.”

Syntax: int array_sum(array $array)

Example:

$sum = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
print array_sum($sum);
Output:
55
In this example the values in the array are added together.
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

 

array_unique

removes duplicate values from an array Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() sorts the values treated as a string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.”

 

Syntax: array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001,
'first_name' => 'Thesis' , 'lastname' => 'Scientist' ,
'address' => '101 South Street' , 'city' => 'Paris' ,
'state' => 'GA' , 'zipcode' => 30001);
print_r(array_unique($first_array));
Output:
Array (
[first_name] => Thesis
[last_name] => Smith
[address] => 101 South Street
[city] => Paris
[state] => GA
[zip_code] => 30001
[lastname] => Scientist
)

 

This function returns the first unique key for each value. In the example, one occurrence of each key is returned.

array_values

returns all the values of an array “Returns an indexed array of values.”

 

Syntax: array array_values ( array $array )

Example:

$first_array = array('first_name' => 'Steve' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
print_r(array_values($first_array));
Output:
Array (
[0]
=> Steve
[1]
=> Smith
[2]
=> 123 Main Street
[3]
=> Paris
[4]
=> CA
[5]
=> 30001
)

array_values builds a numerical array of the values in the passed array. It does not retain the keys (subscripts) from the original array.

 

array_walk_recursive

applies a user function recursively to every member of an array “Applies the user-defined callback function to each element of the array. This function will recurse into deeper arrays.”

 

Syntax: bool array_walk_recursive ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Example:

$customer_record = array (
'first_customer' =>
array('first_name' => 'Steve' , 'last_name' => 'Smith' , 'address' => '123 Main Street' ,
'city' => 'Paris', 'state' => 'GA', 'zip_code' => 30001),
'second_customer' =>
array('first_name' => 'Thesis' , 'last_name' => 'Scientist' , 'address' => '101 South Street' ,
'city' => 'Paris' , 'state' => 'GA' , 'zip_code' => 30001)
);
function print_customer_info($value, $key)
{
print " Customer $key is $value<br>";
}
array_walk_recursive($customer_record,
'print_customer_info');
Output:
Customer first_name is Steve
Customer last_name is Smith
Customer address is 123 Main Street
Customer city is Paris
Customer state is GA
Customer zip_code is 30001
Customer first_name is Thesis
Customer last_name is Scientist
Customer address is 101 South Street
Customer city is Paris
Customer state is GA
Customer zip_code is 30001

Array_walk_recursive will apply anything within the programmer-supplied function to each key and value in an array. As seen in this example, this function works well with multidimensional arrays.

 

array_walk

array_walk

applies a user-supplied function to every member of an array “array_walk() is not affected by the internal array pointer of the array. array_walk() will walk through the entire array regardless of pointer position.”

 

Syntax: bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
function print_customer_info($value, $key)
{
print " Customer $key is $value<br>";
}
array_walk($first_array, 'print_customer_info');
Output:
Customer first_name is Steve
Customer last_name is Smith
Customer address is 123 Main Street
Customer city is Paris
Customer state is CA
Customer zip_code is 30001

Array_walk will apply anything within the programmer-supplied function to each key and value in the array. This function works best with single arrays as shown in the example.

 

count

counts all elements in an array, or something in an object “Returns the number of elements in array_or_countable. If the parameter is not an array or not an object with the implemented Countable interface, 1 will be returned.”

 

Syntax: int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

Example:

$first_array = array('first_name' => 'Steve' ,

'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);

print count($first_array);

count returns the number of elements (size) of the array. In this example, there are six elements. See size for another example that can be used with the count.

 

extract

imports variables into the current symbol table from an array

Syntax: int extract ( array &$array [, int $flags = EXTR_OVERWRITE [, string $prefix = NULL ]] )

Example:

$first_array = array('first_name' => 'Steve' ,
'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Paris', 'state' => 'CA', 'zip_code' => 30001);
extract($first_array);
print "The first name is " . $first_name . ".
The last name is " . $last_name . ". The address is " .
$address . ". The city is " . $city . ". The state is " . $state .
". The zip is " . $zip_code . ".";

 

Output:

The first name is Steve. The last name is Smith. The address is 123 Main Street. The city is Paris. The state is CA. The zip is 30001.

This function works with associative arrays. Variables (properties) are created using the key from the array. The values in the array are then placed into the properties. In this example, six properties are created using the extract function.

size of

alias of count()

Syntax: int sizeof ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

Example:

$first_array = array('Steve' ,'Smith' ,
'123 Main Street' ,'Paris', 'CA', 30001);
$count = sizeof($first_array);
for($I=0; $I < $count; $I++)
{
print $first_array[$I] . "<br />";
}
Output:
Steve
Smith
123 Main Street
Paris
CA
30001

sizeof will return the size of the array. Arrays are numbered starting at zero. The last position would be one less than the value returned. Thus, the for loop used in the preceding continues until $I is no longer less than the value in $count.

 

Notice the example determines the size before the loop, instead of inside the for a loop. This provides only one execution of the size of function instead of calling it seven times in the preceding example. See count for another example.

Recommend