在php中合并多个数组
以下是返回的示例数组值: 这是第一个数组:在php中合并多个数组,php,arrays,Php,Arrays,以下是返回的示例数组值: 这是第一个数组: Array 1: 0 => name => Test name value desrciption => Test description value category => Test category value code_1 => IGEF001 code_2 => IGGF001 1 => name =&g
Array 1:
0 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF001
code_2 => IGGF001
1 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF003
code_2 => IGGF003
2 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF004
code_2 => IGGF004
这是第二个数组:
Array 2:
0 =>
return_code => IGEF003
1 =>
return_code => IGGF003
2 =>
return_code => IGGF004
3 =>
return_code => IGEF004
4 =>
return_code => IGGF001
5 =>
return_code => IGEF001
以下是我试图实现的目标:
0 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF001
code_2 => IGGF001
select_code_1 => IGEF001 <-- Value coming from the second array
select_code_2 => IGGF001 <-- Value coming from the second array
1 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF003
code_2 => IGGF003
select_code_1 => IGEF003 <-- Value coming from the second array
select_code_2 => IGGF003 <-- Value coming from the second array
2 =>
name => Test name value
desrciption => Test description value
category => Test category value
code_1 => IGEF004
code_2 => IGGF004
select_code_1 => IGEF004 <-- Value coming from the second array
select_code_2 => IGGF004 <-- Value coming from the second array
0=>
name=>测试名称值
desrciption=>测试描述值
类别=>测试类别值
代码_1=>IGEF001
代码_2=>IGGF001
选择_代码_1=>IGEF001 IGGF001
name=>测试名称值
desrciption=>测试描述值
类别=>测试类别值
代码_1=>IGEF003
代码_2=>IGGF003
选择_代码_1=>IGEF003 IGGF003
name=>测试名称值
desrciption=>测试描述值
类别=>测试类别值
代码_1=>IGEF004
代码_2=>IGGF004
选择_code_1=>IGEF004 IGGF004如果我正确理解了您的问题,您希望在第二个数组中找到匹配的条目,并将它们添加到第一个数组中。为了简化这个示例,我假设匹配项总是存在的。如果不是这样,您需要添加一个If
和array\u key\u exists()
$result = array();
foreach ($array1 as $key => $value) {
$result = $value;
$result['select_code'] = $array2[$key]['return_code'];
}
我不知道结果中的第二个“select_code”条目应该在哪里找到。据我所知,将第一个数组中的条目映射到第二个数组中的条目的唯一方法是键。如果您对数据的性质有更多的了解,我将编辑我的答案。更不用说我找到了解决方案。我所做的是在for循环中为第一个数组调用查询并链接这些值。因此,我决定创建一个单独的数组,而不是两个单独的数组,这就成功了
谢谢你的帮助 您是否在第二个数组中添加了select_code多少?这是不真实的结构-0=>return_code=>IGEF003
基本上,数据来自两个单独的表。我正在尽量减少数据库的使用。我给你举个例子。。非常感谢。