user1080247
user1080247

Reputation: 1166

How to replace array keys with another array values

I have two arrays and I want to replace the second array keys with the first array values if both keys matches.

As an example: Replace A with Code And B with name

How to do this;

<?php

        $array = array('A' => 'code', 'B' =>'name');
        $replacement_keys = array
        (
            array("A"=>'sara','B'=>2020),
            array("A"=>'ahmed','B'=>1010)

        );
        foreach($replacement_keys as $key => $value){
                foreach($value as $sk => $sv){
                    foreach($array as $rk => $rv){
                      if($sk == $rk ){
                          $sk = $rv;
                      }
                    }

                }

        }
        echo "<pre>";
        print_r($value);
        echo "</pre>";
        exit;

I want the result to be like this

 array(

      [0] => Array
                (
                    [name] => ahmed
                    [code] => 1020
                )

      [1] => Array
                (
                  [name] => sara
                  [code] => 2020
        )

)

Upvotes: 1

Views: 3154

Answers (5)

AnyQuestionsAreGood
AnyQuestionsAreGood

Reputation: 51

I am going to show you a method allowing you to add a prefix to all of my keys. This is an example but illustrates a method to modify the keys of an array in PHP using simple function in php.

Exemple :

I propose the following table:

$vecteur = array();
$vecteur['mykey1'] = 'myValue1';
$vecteur['mykey2'] = 'myValue2';
$vecteur['mykey3'] = 'myValue3';

My goal is to have the following table:

$vecteur = array();
$vecteur['var_mykey1'] = 'myValue1';
$vecteur['var_mykey2'] = 'myValue2';
$vecteur['var_mykey3'] = 'myValue3';

I present to you my solution which works very well. Given that the keys and values come from the same initial array then we are sure that the sizes of the keys and values are the same. This ensures that this method works properly.

Methode : Step 1: we retrieve the values and keys of an array

$allkeys = array_keys($vecteur);
$allvalues = array_values($vecteur);

Step 2: We manipulate the keys in the form of a character string

$allkeys = 'var_'.implode(',var_',$allkeys);

Step 3: We transform the keys which were in a character string into an array

$allkeys = explode(',',$allkeys);

Step 4: We associate the new keys with the values of the initial table

$vecteur = array_combine($allkeys,$allvalues);

Enjoy this method End :)

Upvotes: 0

Shesh Kumar Mishra
Shesh Kumar Mishra

Reputation: 21

array_fill_keys

(PHP 5 >= 5.2.0, PHP 7)

array_fill_keys — Fill an array with values, specifying keys

Description

array array_fill_keys ( array $keys , mixed $value )
  • Fills an array with the value of the value parameter, using the values of the keys array as keys.

http://php.net/manual/en/function.array-fill-keys.php

Upvotes: -1

littleibex
littleibex

Reputation: 1712

<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
    array("A"=>'sara','B'=>2020),
    array("A"=>'ahmed','B'=>1010)

);

foreach($replacement_keys as &$value)
{
    foreach ($array as $key => $name) {
        $value[$name] = $value[$key];
        unset($value[$key]);
    }

}
var_dump($replacement_keys);

Upvotes: 4

Rizier123
Rizier123

Reputation: 59681

This should work for you, nice and simple (I'm going to assume that A should be name and B should be code):

(Here I go through each array from $replacement_keys with array_map() and replace the array_keys() with the array_values() of $array. Then I simply get all array values from $replacement_keys and finally I array_combine() the replaced array keys with the corresponding array values)

$result = array_map("array_combine", 
            array_map(function($v)use($array){
                return str_replace(array_keys($array), array_values($array), array_keys($v));
            }, $replacement_keys),
            $replacement_keys
        );

output:

Array ( [0] => Array ( [code] => sara [name] => 2020 ) [1] => Array ( [code] => ahmed [name] => 1010 ) )

Upvotes: 1

Mex
Mex

Reputation: 999

Try this:

<?php
$array = array('A' => 'code', 'B' =>'name');
$replacement_keys = array
(
    array("A"=>'sara','B'=>2020),
    array("A"=>'ahmed','B'=>1010)

);

$newArray = array();
foreach($replacement_keys as $key => $value)
{
    foreach($value as $key2 => $value2)
    {
        if(isset($array[$key2]))
        {
            $newArray[$key][$array[$key2]] = $value2;
        }
        else
        {
            $newArray[$key][$key2] = $value2;   
        }
    }
}
print_R($newArray);

Upvotes: 2

Related Questions