在一个阵列取决于它们的值替换元素 [英] Replace elements in an array depending on their value

查看:119
本文介绍了在一个阵列取决于它们的值替换元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部嵌套数组的数组。每个阵列都具有它有一个五位数作为它的值称为CBSA的元素。在不同的阵列,我有五位数字作为键,对应于它们的值作为其值。欲环路直通嵌套阵列和替换与来自第二阵列的相应值的五位值。例如:

  $阵列=阵列(阵列(CBSA => 12345),
               阵列(CBSA => 98765),
               阵列(CBSA => 56789));

而在另一个数组我有这样的:

  $数组2 =阵列(12345 =>中乔治·华盛顿
                56789 => 巴拉克奥巴马,
                98765 => 托马斯·杰斐逊);

我想找到的值每个嵌套阵列中,并与所述第二阵列中的相应的字符串代替它。

,使得第一阵列将变成:

  $阵列=阵列(阵列(CBSA =>中乔治·华盛顿),
               阵列(CBSA =>中托马斯·杰斐逊),
               阵列(CBSA =>中美国总统奥巴马));


解决方案

最好的办法是遍历你的第一个数组,然后就分配给它从第二个数组对应的值......像这样

  $结果=阵列();的foreach($数组$关键=> $ CBSA){
    如果(使用isset($数组2 [$ CBSA ['CBSA'])){
        $阵列[$关键] ['CBSA'] =
        $结果[] =阵列('CBSA'=> $数组2 [$ CBSA ['CBSA']);
    }
}

作为使用初始code和上述环的完整工作示例,这得到所需的输出:

 < PHP$阵列=阵列(阵列('CBSA'=> 12345),
               阵列('CBSA'=> 98765),
               阵列('CBSA'=> 56789));$数组2 =阵列(12345 =>中乔治·华盛顿
                56789 => 巴拉克奥巴马,
                98765 => 托马斯·杰斐逊);$结果=阵列();的foreach($数组$关键=> $ CBSA){
    如果(使用isset($数组2 [$ CBSA ['CBSA'])){
        $结果[] =阵列('CBSA'=> $数组2 [$ CBSA ['CBSA']);
    }
}
回声('< pre>');
的print_r($结果);

I have an array with nested arrays inside. Each array has an element called CBSA which has a five digit number as its value. In a different array, I have the five digit number as keys, and the values corresponding to them as their values. I want to loop thru the nested array and replace the five digit value with their corresponding values from the second array. For example:

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

And in another array I have this:

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

I want to find the values in each nested array and replace it with the corresponding string in the second array.

So that the first array will become:

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

解决方案

The best way would be to loop through your first array, then just assign it the corresponding value from the second array... something like this

$results = array();

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

As a full working example using your initial code, and the above loop, this gives the desired output:

<?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);

这篇关于在一个阵列取决于它们的值替换元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆