skip to Main Content

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


  1. You can use array_filter() with anonymous function to compare TD_CODE and crs_oracle_id

    $array_c = array_filter($array_a, function($e) use($array_b) {
        foreach ($array_b as $v) {
            if ($v['crs_oracle_id'] == $e['TD_CODE']) {
                return false;
            }
        }
        return true;
    });
    
    print_r($array_c);
    
    Login or Signup to reply.
  2. Get ids from second array which needs to be rejected, then add only those records which doesn’t exists in this id array,

    $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 — Return the values from a single column in the input array

    Here is working demo

    EDIT

    Here is more optimized code,

    $c = array_filter($a, function($v,$k) use($rejected_ids){
        return !in_array($v['TD_CODE'], $rejected_ids);
    },ARRAY_FILTER_USE_BOTH);
    

    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

    Login or Signup to reply.
  3. If your PHP Version is below 5.5 Then Please use this function

    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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search