Giorgio
Giorgio

Reputation: 1603

Change array keys according to another one

I have 2 arrays:

$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc", 8 => "ddd", 9 => "eee", 11 => "fff");
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "", 9 => "", 13 => ""); 

I want to change the keys of $array1 according to $array2. I'm given the information that an element of the second array has to correspond to another of the first one. For example I know that $array2[6] has to correspond to $array1[4].

So I should have all the keys of $array1 changed according to this rule:

$array1 = array(3 => "aaa", 6 => "bbb", 7 => "ccc", 9 => "ddd", 13 => "eee", 2 => "fff");

I don't know how to solve this. I've tried to split the first array where the element given is but I'm stuck.

Upvotes: 0

Views: 107

Answers (4)

pyrospade
pyrospade

Reputation: 8078

You can define a function that maps and constructs a new array.

function transfer_keys($key_array,
                       $value_array) {
    $a = array_map(null, array_keys($key_array),
                   $value_array);
    $result = array();
    foreach($a as $kv) {
        $result[$kv[0]] = $kv[1];
    }
    return $result;
}

$array1 = array(1 => "aaa", 4 => "bbb", 5 => "ccc", 
                8 => "ddd", 9 => "eee", 11 => "fff"); 
$array2 = array(2 => "", 3 => "", 6 => "", 7 => "", 
                9 => "", 13 => "");    

print_r(transfer_keys($array2, $array1));

Upvotes: 1

Jerska
Jerska

Reputation: 12002

if you wanna update all elements of your first array with the values in the array2 in the same order :

$i = 0;
foreach ($array1 as $v) {
    while (!isset($array2[$v]))
        $i++;
    $array2[$i] = $v;
}

But if you want to update according to an order you pre-defined, you have to make something else, like a table defining the rule :

$assoc_array = array(
    2 => 4,
    3 => 7,
    4 => 11,
    6 => 5,
    5 => 9,
    8 => 1);
foreach ($assoc_array as $k => $v) {
    $array1[$k] = $array2[$v];
}        

Hope this helps.

Aw, you know the difference is always the same !

Then this should be something like :

function updateArray ($array1, $array2, $key_of_array1, $key_associated_of_array2) {
    $diff = $key_associated_of_array2 - $key_of_array1;
    foreach ($array1 as $k => $v) {
        $array2[$k + $diff] = $array1[$k];
    } 
}

Upvotes: 0

Raiden
Raiden

Reputation: 484

foreach($array1 as $item => $value)
 if(isset($array2[($item + 2)]) && item != 11)
  $temp[$item + 2] = $value;
 elseif($item == 11)
  $temp[2] = $value;
$array1 = @$temp;

Example code.. but again, you need to tell us the pattern that determines where the first array elements are being inserted into the second array elements. I think it's.. +2? Maybe?

Upvotes: 0

Hardik
Hardik

Reputation: 1411

you can get first keys of both arrays using function array_keys();

suppose, $keys1 contains keys of $array1 and $keys2 cotains keys of $array2

Then move into for loop as follows:

for($i=0 ; $i<count($array1) ; $i++)
{
    $result[$keys2[$i+1]] = $array1[$i];
}

print_r($result);

Hope this will be helpful to you

Upvotes: 0

Related Questions