根据数值替换数组中的元素

时间:2014-01-01 05:37:25

标签: php arrays

我有一个内置嵌套数组的数组。每个数组都有一个名为CBSA的元素,它的值为五位数。在不同的数组中,我将五位数作为键,并将与其对应的值作为其值。我想循环通过嵌套数组并将五位数值替换为第二个数组中的相应值。例如:

$array = array(array(CBSA => 12345),
               array(CBSA => 98765),
               array(CBSA => 56789));

在另一个阵列中我有这个:

$array2 = array(12345 => "George Washington",
                56789=> "Barack Obama",
                98765=> "Thomas Jefferson");

我想在每个嵌套数组中找到值,并将其替换为第二个数组中的相应字符串。

这样第一个数组将成为:

$array = array(array(CBSA=>"George Washington"), 
               array(CBSA=>"Thomas Jefferson"), 
               array(CBSA => "Barack Obama"));

4 个答案:

答案 0 :(得分:0)

最好的方法是遍历你的第一个数组,然后从第二个数组中分配相应的值...就像这样

$results = array();

foreach($array as $key => $cbsa) {
    if (isset($array2[$cbsa['CBSA']])) {
        $array[$key]['CBSA'] = 
        $results[] = array('CBSA' => $array2[$cbsa['CBSA']]);
    }
}

作为使用初始代码和上述循环的完整工作示例,这将提供所需的输出:

<?php

$array = array(array('CBSA' => 12345),
               array('CBSA' => 98765),
               array('CBSA' => 56789));

$array2 = array(12345 => "George Washington",
                56789=> "Barack Obama",
                98765=> "Thomas Jefferson");

$results = array();

foreach($array as $key => $cbsa) {
    if (isset($array2[$cbsa['CBSA']])) {
        $results[] = array('CBSA' => $array2[$cbsa['CBSA']]);
    }
}
echo('<pre>');
print_r($results);

答案 1 :(得分:0)

试试这个: 演示:https://eval.in/84567

$array = array(array('CBSA' => '12345'), array('CBSA' => '98765'), array('CBSA' => '56789'));
$array2 = array('12345' => "George Washington", '56789'=> "Barack Obama", '98765'=> "Thomas Jefferson");
$result = array();
foreach($array as $arr){
foreach($arr as $key => $val){
    array_push($result,array($key => $array2[$val]));
}
}
print_r($result);

答案 2 :(得分:0)

只用一个foreach循环尝试这个

 $array = array(array(CBSA => 12345),
               array(CBSA => 98765),
               array(CBSA => 56789));

 $array2 = array(12345 => "George Washington",
                56789=> "Barack Obama",
                98765=> "Thomas Jefferson");

 $newArray  = array();

 foreach($array as $key => $value) {
      $newArray[] = array('CBSA' => $array2[$value['CBSA']]);
 }

 echo '<pre>';
 print_r($newArray);

答案 3 :(得分:0)

$arrays = array(
        array("CBSA" => 12345),
        array("CBSA" => 98765),
        array("CBSA" => 56789)
    );
$array2 = array(
        12345 => "George Washington",
        56789 => "Barack Obama",
        98765 => "Thomas Jefferson"
    );
/****** Code block ********/
array_walk($arrays, function(&$array) use ($array2)
{
    $arr = array_values($array);
    if(@$array2[$arr[0]]) // $arr[0] contains numeric value
    {
        $key = array_search($arr[0], $array); // contains CBSA
        $array[$key] = $array2[$arr[0]];
    }
});
print_r($arrays);

<强>输出

Array
(
    [0] => Array
        (
            [CBSA] => George Washington
        )

    [1] => Array
        (
            [CBSA] => Thomas Jefferson
        )

    [2] => Array
        (
            [CBSA] => Barack Obama
        )

)