PHP Functions - Comparing and Merging Arrays - Arrays: PHP 7 Complete Guide: Simple, Multi-dimensional, Associative, and Object Arrays (2016)

Arrays: PHP 7 Complete Guide: Simple, Multi-dimensional, Associative, and Object Arrays (2016)

Chapter 5. PHP Functions - Comparing and Merging Arrays

After completing this chapter, the student will be able to…

Create a simple PHP program which compares two arrays on a value, key, or with a user defined function.

Create a simple PHP program which merges two arrays via union or intersection

In this chapter 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, multi-dimensional, and/or associative).

All descriptions of the following functions are provided by the on-line 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 ment to get your feet wet. For a more detailed description of the functions shown, and for more examples, visit the on-line 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'last_name' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zip_code' => 30001);

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

print_r($difference);

Output:

Array

(

[first_name] => Pete

[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] => Sally

[last_name] => Parisi

[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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

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

Output:

array(3)

{

["first_name"]=> string(4) "Pete"

["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) "Sally"

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

["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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'last_name' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , '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] => Pete

[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.

If we switch parameters

Example:

print_r( array_diff_uassoc($second_array,

$first_array, "key_compare"));

Output:

Array (

[first_name] => Sally

[last_name] => Parisi

[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 fromarray1against the keys fromarray2 and returns the difference.”

Syntax:

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

Example:

$first_array = array('first_name' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , '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) "Pete"

["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.

If we switch parameters

Example:

var_dump(array_diff_ukey($second_array, $first_array, 'key_compare'));

Output:

array(3) {

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

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

["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.

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

Output:

Array (

[1] => b

[2] => c

[4] => e

)

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 an user-supplied callback.”

Syntax:

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

Example:

$first_array = array('first_name' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', '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] => Pete

[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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', '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] => Pete

[last_name] => Smith

[state] => CA

[zip_code] => 30001

)

The user must supply a function for both comparing 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 is displayed. If the values are different, the first_array key and value is also displayed. Thus, first_name is displayed because the indexes are different. last_name is displayed because the values are different. 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', '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

)

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 is displayed. Index differences are ignored. The value in last_name and state are different in the two arrays.

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_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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zipcode' => 30001);

print_r(array_uintersect_assoc($first_array, $second_array, "strcasecmp"));

Output:

Array (

[address] => 123 Main Street

[city] => Atlanta

)

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 is returned. Only address and city are match 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', '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] => Atlanta

)

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 andcity 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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'CA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Pete' , 'last_name' => 'Jones' , 'address' => '123 Main Street' , 'city' => 'Atlanta', '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] => Pete

[address] => 123 Main Street

[city] => Atlanta

[zip_code] => 30001

)

This function will ignore a 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

array_combine - creates an array by using one array for keys and another for its values

“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('Pete', 'Sally', 'Fred');

$last_names = array('Smith', 'Parisi', 'Jones');

$names = array_combine($last_names,

$first_names);

print_r($names);

Output:

Array

(

[Smith] => Pete

[Parisi] => Sally

[Parisi] => Sally

)

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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

$result_array = array_intersect_assoc($first_array, $second_array);

print_r($result_array);

Output:

Array (

[city] => Atlanta

[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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

$result_array = array_intersect_key($first_array, $second_array);

print_r($result_array);

Output:

Array (

[address] => 123 Main Street

[city] => Atlanta

[state] => GA

)

This function compares 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

$result_array = array_intersect_uassoc($first_array, $second_array, "strcasecmp");

print_r($result_array);

Output:

Array (

[city] => Atlanta

[state] => GA

)

This example returns the common values Atlanta 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , '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] => Atlanta

[state] => GA

)

The programmer can create their own functions. However, they must return 0 (exact match), < 0 (first array is less than second array), or >0 (first array is greater than 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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , '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] => Atlanta

[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 used PHP functions (such as strcasecmp) instead of your own functions.

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_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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

$result_array = array_intersect($first_array, $second_array);

print_r($result_array);

Output:

Array (

[city] => Atlanta

[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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'last_name' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zip_code' => 30001);

$result = array_merge_recursive($first_array,

$second_array);

print_r($result);

Output:

Array (

[first_name] => Array

( [0] => Pete

[1] => Sally

)

[last_name] => Array

( [0] => Smith

[1] => Parisi

)

[address] => Array

(

[0] => 123 Main Street

[1] => 101 South Street

)

[city] => Array

( [0] => Atlanta

[1] => Atlanta

)

[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 multi-dimensional array with rows (arrays) of like items, such as first_name.

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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'last_name' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zip_code' => 30001);

$result = array_merge($first_array,

$second_array);

print_r($result);

Output:

Array (

[first_name] => Sally

[last_name] => Parisi

[address] => 101 South Street

[city] => Atlanta

[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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('firstname' => 'Sally' , 'lastname' => 'Parisi' , 'add' => '101 South Street' , 'cty' => 'Atlanta' , 'st' => 'GA' , 'zipcode' => 30001);

$result = array_merge($first_array, $second_array);

print_r($result);

Output:

Array (

[first_name] => Pete

[last_name] => Smith

[address] => 123 Main Street

[city] => Atlanta

[state] => GA

[zip_code] => 30001

[firstname] => Sally

[lastname] => Parisi

[add] => 101 South Street

[cty] => Atlanta

[st] => GA

[zipcode] => 30001

)

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

Example:

$first_array = array('Pete' , 'Smith' , '123 Main Street' , 'Atlanta', 'GA', 30001);

$second_array = array('Sally' , 'Parisi' , '101 South Street' , 'Atlanta' ,'GA' , 30001);

$result = array_merge($first_array,

$second_array);

print_r($result);

Output:

Array (

[0] => Pete

[1] => Smith

[2] => 123 Main Street

[3] => Atlanta

[4] => GA

[5] => 30001

[6] => Sally

[7] => Parisi

[8] => 101 South Street

[9] => Atlanta

[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 — 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

print_r(array_replace_recursive($first_array, $second_array));

Output:

Array (

[first_name] => Sally

[last_name] => Smith

[address] => 101 South Street

[city] => Atlanta

[state] => GA

[zip_code] => 30001

[lastname] => Parisi

[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 (lastname, zipcode) 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' => 'Pete' , 'last_name' => 'Smith' , 'address' => '123 Main Street' , 'city' => 'Atlanta', 'state' => 'GA', 'zip_code' => 30001);

$second_array = array('first_name' => 'Sally' , 'lastname' => 'Parisi' , 'address' => '101 South Street' , 'city' => 'Atlanta' , 'state' => 'GA' , 'zipcode' => 30001);

print_r(array_replace($first_array,

$second_array));

Output:

Array (

[first_name] => Sally

[last_name] => Smith

[address] => 101 South Street

[city] => Atlanta

[state] => GA

[zip_code] => 30001

[lastname] => Parisi

[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 (lastname, zipcode) produced. This process is not recursive.

Exercises

1. Create a PHP program which contains an array of cities with a population of greater than 100,000 residents in Georgia. Include a section array which contains cities with a population of greater than 100,000 residents in Florida. Using one of the PHP functions shown in this chapter, compare the arrays and display the common elements.

2. Using the arrays from #1, use on of the functions shown in this chapter to merge the arrays without any duplications existing in the new array.