Array merge key value php

[PHP 5, PHP 7, PHP 8]

array_combineCreates an array by using one array for keys and another for its values

Description

array_combine[array $keys, array $values]: array

Parameters

keys

Array of keys to be used. Illegal values for key will be converted to string.

values

Array of values to be used

Return Values

Returns the combined array.

Errors/Exceptions

As of PHP 8.0.0, a ValueError is thrown if the number of elements in keys and values does not match. Prior to PHP 8.0.0, a E_WARNING was emitted instead.

Changelog

VersionDescription
8.0.0 array_combine[] will now throw a ValueError if the number of elements for each array is not equal; previously this function returned false instead.

Examples

Example #1 A simple array_combine[] example

The above example will output:

Array
[
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
]

See Also

  • array_merge[] - Merge one or more arrays
  • array_walk[] - Apply a user supplied function to every member of an array
  • array_values[] - Return all the values of an array

loureirorg at gmail dot com

9 years ago

If two keys are the same, the second one prevails.

Example:

Returns:
Array
[
    [a] => 2
    [b] => 3
]

But if you need to keep all values, you can use the function below:


Returns:
Array
[
    [a] => Array
        [
            [0] => 1
            [1] => 2
        ]

    [b] => 3
]

welcome at el hyphen mustafa

6 years ago

Further to loreiorg's script
in order to preserve duplicate keys when combining arrays.

I have modified the script to use a closure instead of create_function

Reason: see security issue flagged up in the documentation concerning create_function

claude dot pache at gmail dot com

12 years ago

array_combine[] has a strange bug/misfeature [as of PHP 5.3.2]: There is no logical reason for throwing a warning and returning FALSE, instead of returning [see //bugs.php.net/bug.php?id=34857]. Here is a quick workaround:

quecoder at gmail

14 years ago

zequez at gmail dot com

11 years ago

If two keys are the same, the second one prevails.
Example:

Returns:
Array
[
    [a] => 2
    [b] => 3
]

g.REMOVETHIS.vincendon AT vithemis.com

10 years ago

I was looking for a function that could combine an array to multiple one, for my MySQL GROUP_CONCAT[] query, so I made this function.

dejiakala at gmail dot com

10 years ago

I needed to read CSV files into associative arrays with column headers as keys. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr[] below allowed me to pad either array or not when parsing my CSVs to arrays.
$a is the array of header columns and $b is an array of the current row retrieved with fgetcsv[]



What can you do? In my case, I was just zipping up some select-box options, so I converted everything in my floats to strings.

Anonymous

8 months ago

Here a way to manage no uniqueness keys case. Values of duplicate keys are put in an array.



Results:  Array [ [one] => green [two] => blue ]

NOT:  Array [ [one] => red [one] => green [two] => blue ]

xavier at api-meal dot eu

13 years ago

Chủ Đề