0

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.

5
  • 1
    How can you tell that '$array2[6] has to correspond to $array1[4]' ? Commented Jul 29, 2012 at 17:30
  • I am not getting what you want? please re-explain it Commented Jul 29, 2012 at 17:31
  • @Vatev I know that from another function, but basically that input could vary from time to time, I should be able to change the keys of the first array in every occurrence (it could be array2[13] has to correspond to $array1[9] or whatever). Note that both arrays have the same number of elements. Commented Jul 29, 2012 at 17:39
  • This doesn't make sense because you haven't told us the pattern. Commented Jul 29, 2012 at 17:39
  • @Raiden I bet it's my poor English :) However given the starting point: eg $a2[6] = $a1[4], all the other elements of the first array should be set according to that with the keys taken from the second array Commented Jul 29, 2012 at 17:41

4 Answers 4

1

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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];
    } 
}

Comments

0

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

Comments

0
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?

2 Comments

There is no pattern between the keys of each array. BUT the distance between two elements of the same array is always the same. EG $a1 --> 1 3 4 7 11 $a2 --> 70 73 74 77 81
If I know that 73-->11 I'll change the numbers in the first array to $a1 --> 74 77 81 70 73

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.