我有2个数组:
阵列A
Array
(
[0] => Array
(
[TD_CODE] => 24203
[CRS_NAME] => Adobe Photoshop CS6+CC
)
[1] => Array
(
[TD_CODE] => 24202
[CRS_NAME] => Advance Microsoft excel 2010/2007
)
[2] => Array
(
[TD_CODE] => 24197
[CRS_NAME] => Beginning Auditor Tools and Techniques
)
);
和阵列B
Array
(
[0] => Array
(
[crs_id] => 1
[crs_ia_id] => 2017-6495
[crs_oracle_id] => 24653
[crs_name] => Windows8
[crs_start_date] => 2017-08-07
[crs_end_date] => 2017-08-11
)
[1] => Array
(
[crs_id] => 2
[crs_ia_id] => 2017-5013
[crs_oracle_id] => 24202
[crs_name] => Advance Microsoft excel 2010/2007
[crs_start_date] => 2017-02-08
[crs_end_date] => 2017-02-09
)
)
我想让数组A成为一个不同的数组B.
条件是使用数组A 的TD_CODE
比较到数组b的crs_oracle_id
并将其作为数组C。
所以结果如下。
Array
(
[0] => Array
(
[TD_CODE] => 24203
[CRS_NAME] => Adobe Photoshop CS6+CC
)
[1] => Array
(
[TD_CODE] => 24197
[CRS_NAME] => Beginning Auditor Tools and Techniques
)
);
我该怎么办?
答案 0 :(得分:2)
您可以使用array_filter()
匿名函数来比较NULL
和TD_CODE
crs_oracle_id
答案 1 :(得分:1)
从第二个数组获取需要被拒绝的ID,然后只添加这个id数组中不存在的记录,
$rejected_ids = array_column($b,'crs_oracle_id');
$c = [];
foreach($a as $v){
if(!in_array($v['TD_CODE'], $rejected_ids)){
$c[] = $v;
}
}
print_r($c);
array_column - 返回输入数组中单个列的值
这是有效的demo
编辑
这是更优化的代码,
$c = array_filter($a, function($v,$k) use($rejected_ids){
return !in_array($v['TD_CODE'], $rejected_ids);
},ARRAY_FILTER_USE_BOTH);
以下是demo。
array_filter - 使用回调函数过滤数组元素
ARRAY_FILTER_USE_BOTH - 将值和键作为参数传递给回调而不是值
答案 2 :(得分:0)
如果您的PHP版本低于5.5那么请使用此功能
if (! function_exists('array_column')) {
function array_column(array $input, $columnKey, $indexKey = null) {
$array = array();
foreach ($input as $value) {
if ( !array_key_exists($columnKey, $value)) {
trigger_error("Key \"$columnKey\" does not exist in array");
return false;
}
if (is_null($indexKey)) {
$array[] = $value[$columnKey];
}
else {
if ( !array_key_exists($indexKey, $value)) {
trigger_error("Key \"$indexKey\" does not exist in array");
return false;
}
if ( ! is_scalar($value[$indexKey])) {
trigger_error("Key \"$indexKey\" does not contain scalar value");
return false;
}
$array[$value[$indexKey]] = $value[$columnKey];
}
}
return $array;
}
}
ARRAY - A
$array_a = Array
(
'0' => Array
(
'TD_CODE' => '24203',
'CRS_NAME' => 'Adobe Photoshop CS6+CC'
),
'1' => Array
(
'TD_CODE' => '24202',
'CRS_NAME' => 'Advance Microsoft excel 2010/2007'
),
'2' => Array
(
'TD_CODE' => '24197',
'CRS_NAME' => 'Beginning Auditor Tools and Techniques'
)
);
ARRAY - B
$array_b = Array
(
'0' => Array
(
'crs_id' => '1',
'crs_ia_id' => '2017-6495',
'crs_oracle_id' => '24653',
'crs_name' => 'Windows8',
'crs_start_date' => '2017-08-07',
'crs_end_date' => '2017-08-11'
),
'1' => Array
(
'crs_id' => '2',
'crs_ia_id' => '2017-5013',
'crs_oracle_id' => '24202',
'crs_name' => 'Advance Microsoft excel 2010/2007',
'crs_start_date' => '2017-02-08',
'crs_end_date' => '2017-02-09'
)
);
ARRAY - C
$array_c = array();
foreach ($array_a as $a){
if(!in_array($a['TD_CODE'], array_column($array_b, 'crs_oracle_id'))) {
$array_c[] = array('TD_CODE' => $a['TD_CODE'],'CRS_NAME'=>$a['CRS_NAME']);
}
}
print_r($array_c);