Replace key with value in array php

I have the following array:

array['Elnett', 'INOA INOA', 'Playball P', 'Preferred Color Specialist', 
      'Série Expert', 'Série Nature', 'Techni art']

I would like to have keys and values like:

array['Elnett' => 'Elnett', 
      'INOA INOA' => 'INOA INOA', 
      'Playball P' => 'Playball', 
      'Preferred Color Specialis' => 'Preferred Color Specialist', 
      'Série Expert' => 'Série Expert', 
      'Série Nature' => 'Série Nature', 
      'Techni art' => 'Techni art']

How can I accomplish this?

[PHP 5 >= 5.3.0, PHP 7, PHP 8]

array_replaceReplaces elements from passed arrays into the first array

Description

array_replace[array $array, array ...$replacements]: array

array_replace[] is not recursive : it will replace values in the first array by whatever type is in the second array.

Parameters

array

The array in which elements are replaced.

replacements

Arrays from which elements will be extracted. Values from later arrays overwrite the previous values.

Return Values

Returns an array.

Examples

Example #1 array_replace[] example

The above example will output:

Array
[
    [0] => grape
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => cherry
]

See Also

  • array_replace_recursive[] - Replaces elements from passed arrays into the first array recursively
  • array_merge[] - Merge one or more arrays

steelpandrummer

10 years ago

marvin_elia at web dot de

8 years ago

Simple function to replace array keys. Note you have to manually select wether existing keys will be overrided.

/**
  * @param array $array
  * @param array $replacements
  * @param boolean $override
  * @return array
  */
function array_replace_keys[array $array, array $replacements, $override = false] {
    foreach [$replacements as $old => $new] {
        if[is_int[$new] || is_string[$new]]{
            if[array_key_exists[$old, $array]]{
                if[array_key_exists[$new, $array] && $override === false]{
                    continue;
                }
                $array[$new] = $array[$old];
                unset[$array[$old]];
            }
        }
    }
    return $array;
}

ali dot sweden19 at yahoo dot com

6 years ago

Here is a simple array_replace_keys function:

/**
     * This function replaces the keys of an associate array by those supplied in the keys array
     *
     * @param $array target associative array in which the keys are intended to be replaced
     * @param $keys associate array where search key => replace by key, for replacing respective keys
     * @return  array with replaced keys
     */
    private function array_replace_keys[$array, $keys]
    {
        foreach [$keys as $search => $replace] {
            if [ isset[$array[$search]]] {
                $array[$replace] = $array[$search];
                unset[$array[$search]];
            }
        }

        return $array;
    }

// Test Drive

print_r[array_replace_keys[['one'=>'apple', 'two'=>'orange'], ['one'=>'ett', 'two'=>'tvo']];
// Output
array[
'ett'=>'apple',
'tvo'=>'orange'
]

mail at romansklenar dot cz

12 years ago

To get exactly same result like in PHP 5.3, the foreach loop in your code should look like:



Check on this code:



Function array_replace "replaces elements from passed arrays into the first array" -- this means replace from top-right to first, then from top-right - 1 to first, etc, etc...

gmastro77 at gmail dot com

9 years ago

In some cases you might have a structured array from the database and one
of its nodes goes like this;



Now consider that you want to capture $arr values from $keys.
Assuming that you have a limitation to display the content into given keys
order, i.e. use it with a vsprintf, you could use the following



I hope that this will save someone's time.

polecat at p0lecat dot com

11 years ago

I got hit with a noob mistake. :]

When the function was called more than once, it threw a function redeclare error of course.  The enviroment I was coding in never called it more than once but I caught it in testing and here is the fully working revision.  A simple logical step was all that was needed.

With PHP 5.3 still unstable for Debian Lenny at this time and not knowing if array_replace would work with multi-dimensional arrays, I wrote my own.  Since this site has helped me so much, I felt the need to return the favor. :]

Chủ Đề