I have 2 arrays follow this:
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
)
);
And 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
)
)
I want to make array A different array B.
The condition is to use TD_CODE
of the array A compared to crs_oracle_id
of array b And and take it as array C.
So The results are as follows.
Array
(
[0] => Array
(
[TD_CODE] => 24203
[CRS_NAME] => Adobe Photoshop CS6+CC
)
[1] => Array
(
[TD_CODE] => 24197
[CRS_NAME] => Beginning Auditor Tools and Techniques
)
);
How should I do?
3
Answers
You can use
array_filter()
with anonymous function to compareTD_CODE
andcrs_oracle_id
Get ids from second array which needs to be rejected, then add only those records which doesn’t exists in this id array,
array_column — Return the values from a single column in the input array
Here is working demo
EDIT
Here is more optimized code,
Here is working demo.
array_filter — Filters elements of an array using a callback function
ARRAY_FILTER_USE_BOTH – pass both value and key as arguments to callback instead of the value
If your PHP Version is below 5.5 Then Please use this function
ARRAY – A
ARRAY – B
ARRAY – C