比较并替换数组中的值

I need compare 2 arrays , the first array have one order and can´t change , in the other array i have different values , the first array must compare his id with the id of the other array , and if the id it´s the same , take the value and replace for show all in the same order

For Example :

$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");

The Result in this case i want get it´s this :

"1a-dogs","2a-cats","3a-birds","4a-walking"

If the id in this case 4a it´s the same , that entry must be modificate and put the value of other array and stay all in the same order

I do this but no get work me :

for($fte=0;$fte<count($array_1);$fte++)
{
  $exp_id_tmp=explode("-",$array_1[$fte]);  
  $cr_temp[]="".$exp_id_tmp[0]."";  
}   


for($ftt=0;$ftt<count($array_2);$ftt++)
{
  $exp_id_targ=explode("-",$array_2[$ftt]);
  $cr_target[]="".$exp_id_targ[0]."";   
}

/// Here I tried use array_diff and others but no can get the results as i want 

How i can do this for get this results ?

Maybe you could use the array_udiff_assoc() function with a callback

First split the 2 arrays into proper key and value pairs (key = 1a and value = dogs). Then try looping through the first array and for each of its keys check to see if it exists in the second array. If it does, replace the value from the second array in the first. And at the end your first array will contain the result you want.

Like so:

$array_1 = array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2 = array("4a-walking","2a-cats");

function splitArray ($arrayInput)
{
    $arrayOutput = array();
    foreach ($arrayInput as $element) {
        $tempArray = explode('-', $element);
        $arrayOutput[$tempArray[0]] = $tempArray[1];
    }
    return $arrayOutput;
}

$arraySplit1 = splitArray($array_1);
$arraySplit2 = splitArray($array_2);

foreach ($arraySplit1 as $key1 => $value1) {
    if (array_key_exists($key1, $arraySplit2)) {
        $arraySplit1[$key1] = $arraySplit2[$key1];
    }
}

print_r($arraySplit1);

See it working here: http://3v4l.org/2BrVI

Here you go. It's not the cleanest code I've ever written.

Runnable example: http://3v4l.org/kUC3r

<?php
$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");

function getKeyStartingWith($array, $startVal){
    foreach($array as $key => $val){
        if(strpos($val, $startVal) === 0){
            return $key;
        }
    }
    return false;
}

function getMergedArray($array_1, $array_2){
    $array_3 = array();
    foreach($array_1 as $key => $val){
        $startVal = substr($val, 0, 2);
        $array_2_key = getKeyStartingWith($array_2, $startVal);
        if($array_2_key !== false){
            $array_3[$key] = $array_2[$array_2_key];
        } else {
            $array_3[$key] = $val;
        }
    }
    return $array_3;
}

$array_1 = getMergedArray($array_1, $array_2);

print_r($array_1);
$array_1=array("1a-dogs","2a-cats","3a-birds","4a-people");
$array_2=array("4a-walking","2a-cats");

function merge_array($arr1, $arr2) {
    $arr_tmp1 = array();
    foreach($arr1 as $val) {
        list($key, $val) = explode('-', $val);
        $arr_tmp1[$key] = $val;
    }
    foreach($arr2 as $val) {
        list($key, $val) = explode('-', $val);
        if(array_key_exists($key, $arr_tmp1))
            $arr_tmp1[$key] = $val;
    }

    return $arr_tmp1;
}

$result = merge_array($array_1, $array_2);

echo '<pre>';
print_r($result);
echo '</pre>';

This short code works properly, you'll get this result:

Array
(
    [1a] => dogs
    [2a] => cats
    [3a] => birds
    [4a] => walking
)