0

I have an reference array like this (the data amount is dynamic, about 3000 records)

Array
(
    [0] => Array
        (
            [DEPT] => FIN
            [KEYCODE] => AAAAA
            [TYPE] => 7
        )

    [1] => Array
        (
            [DEPT] => SALE
            [KEYCODE] => BBBBB
            [TYPE] => 16
        )

    [2] => Array
        (
            [DEPT] => SCM
            [KEYCODE] => CCCCC
            [TYPE] => 1
        )
)

and use the value of "TYPE" to search keys "[9][7][16][1]" and value of "KEYCODE" to search value of above keys in following array. (dynamic, about 5000 records)

Array
(
    [0] => Array
        (
            [NAME] => Nick
            [9] => GGGGG
            [7] => AAAAA
            [16] => MMMMM
            [1] => KKKKK
        )
    [1] => Array
        (
            [NAME] => Chris
            [9] => PPPPP
            [7] => BBBBB
            [16] => ZZZZZ
            [1] => RRRRR
        )
    [2] => Array
        (
            [NAME] => Cathy
            [9] => SSSSS
            [7] => UUUUU
            [16] => JJJJJ
            [1] => AAAAA
        )
    [3] => Array
        (
            [NAME] => Allen
            [9] => FFFFF
            [7] => DDDDD
            [16] => WWWWW
            [1] => CCCCC
        )
)

the output array should be

Array
(
    [0] => Array
        (
            [NAME] => Chris
            [9] => PPPPP
            [7] => HHHHH
            [16] => ZZZZZ
            [1] => RRRRR
        )
    [1] => Array
        (
            [NAME] => Cathy
            [9] => SSSSS
            [7] => UUUUU
            [16] => JJJJJ
            [1] => XXXXX
        )
)

"Nick" have item [7]=> AAAAA and "Allen" have item [1]=> CCCCC, so delete from data array. I know the way to delete array item, but no idea to implement compare and search by key/value pair from other array.

4
  • If I'm not mistaken, you want to delete items from the second array if the search function matches the [KEYCODE] and [TYPE] ?? Commented Nov 25, 2020 at 7:56
  • yes, delete [NAME] [9] [7] [16] [1] if finding total match item of [KEYCODE] and [TYPE] Commented Nov 25, 2020 at 7:59
  • You want to find the KEYCODE from the first array based on TYPE and then delete a whole record in the second one based on the found KEYCODE? Can there be multiple values of KEYCODE for the same TYPE? Commented Nov 25, 2020 at 8:17
  • [TYPE, KEYCODE] at first array is unique. But in second array may have same "TYPE" but different "KEYCODE", I edited the second array for more closer real data array. Commented Nov 25, 2020 at 8:45

1 Answer 1

2

For what I understand from your question, here's a simple solution for your required task:

    $firstArr = array(
        0 => array(
            "DEPT" => 'FIN',
            'KEYCODE' => 'AAAAA',
            'TYPE' => 7
        ),
        1 => array(
            'DEPT' => 'SALE',
            'KEYCODE' => 'BBBBB',
            'TYPE' => 16
        ),
        2 => array(
            'DEPT' => 'SCM',
            'KEYCODE' => 'CCCCC',
            'TYPE' => 1
        )
    );

    $secondArr = array(
        0 => array(
            'NAME' => 'Nick',
            9 => 'GGGGG',
            7 => 'AAAAA',
            16 => 'MMMMM',
            1 => 'KKKKK',
        ),
        1 => array(
            'NAME' => 'Chris',
            9 => 'PPPPP',
            7 => 'HHHHH',
            16 => 'ZZZZZ',
            1 => 'RRRRR'
        ),
        2 => array(
            'NAME' => 'Cathy',
            9 => 'SSSSS',
            7 => 'UUUUU',
            16 => 'JJJJJ',
            1 => 'XXXXX'
        ),
        3 => array(
            'NAME' => 'Allen',
            9 => 'FFFFF',
            7 => 'DDDDD',
            16 => 'WWWWW',
            1 => 'CCCCC'
        )
    );

    foreach($firstArr as $row){
        foreach($secondArr as $i => $que){
            if(array_key_exists($row['TYPE'], $que) && $row['KEYCODE'] == $que[$row['TYPE']]){
                unset($secondArr[$i]); // deleting selected item from the array...
            }
        }
    }

    echo '<pre>'; 
    print_r($secondArr);
    echo '</pre>';

Output of the above code:

Array
(
    [1] => Array
    (
        [NAME] => Chris
        [9] => PPPPP
        [7] => HHHHH
        [16] => ZZZZZ
        [1] => RRRRR
    )

[2] => Array
    (
        [NAME] => Cathy
        [9] => SSSSS
        [7] => UUUUU
        [16] => JJJJJ
        [1] => XXXXX
    )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.