skip to Main Content

How can I merge 2 different arrays with different keys based on a shared key’s value?
I am trying to keep as basic the first array.
If in the second array, shared key’s(Mark) value is the same, merge with first array.

First array:

Array
(
    [0] => Array
        (
            [IssueDate] => 2024-01-22
            [Mark] => 400001923264133
            [Vat301] => 0
            [Vat331] => 0
            [Vat302] => 0
            [Vat332] => 0
            [Vat303] => 0
            [Vat333] => 0
            [Vat304] => 0
            [Vat334] => 0
        )

    [1] => Array
        (
            [IssueDate] => 2024-01-22
            [Mark] => 400001923536194
            [Vat301] => 0
            [Vat331] => 0
            [Vat302] => 0
            [Vat332] => 0
            [Vat303] => 0
            [Vat333] => 0
            [Vat304] => 0
            [Vat334] => 0
        )

)

Second array:

Array
(
    [0] => Array
        (
            [uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
            [Mark] => 400001923264133
            [vn] => 094019245
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )

        )

    [1] => Array
        (
            [uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
            [Mark] => 400001923536194
            [vn] => 094019245
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )

        )

    [2] => Array
        (
            [uid] => 74820E25D29945A74408F64FCBEAE229D4D386FF
            [Mark] => 400001923801925
            [vn] => 094019245
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )

        )

    [3] => Array
        (
            [uid] => F218FCCF5A12CA067221D4C783ABC52B0A305ACA
            [Mark] => 400001924018534
            [vn] => 094019245
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )

        )

)

I want to add to first array, second array’s keys and values if key[‘Mark’] contains the same value.

Expected Output
Third array:

Array
(
    [0] => Array
        (
            [uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
            [vn] => 094019245
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )       
            [IssueDate] => 2024-01-22
            [Mark] => 400001923264133
            [Vat301] => 0
            [Vat331] => 0
            [Vat302] => 0
            [Vat332] => 0
            [Vat303] => 0
            [Vat333] => 0
            [Vat304] => 0
            [Vat334] => 0
        )

    [1] => Array
        (
            [uid] => 0D7F298912F21E7934380E5728AEF6E31AB74E3F
            [Mark] => 400001923536194
            [series] => 0
            [aa] => 396663852
            [name] => string
                (
                    [name] => TYPE_2_1
                    [value] => 2.1
                )

            [netValue] => 976.15
            [vatAmount] => 246.50
            [vatCategory] => int
                (
                    [name] => VAT_1
                    [value] => 1
                )       
            [IssueDate] => 2024-01-22
            [Mark] => 400001923536194
            [Vat301] => 0
            [Vat331] => 0
            [Vat302] => 0
            [Vat332] => 0
            [Vat303] => 0
            [Vat333] => 0
            [Vat304] => 0
            [Vat334] => 0
        )

)

2

Answers


  1. Chosen as BEST ANSWER
    $result = [];
    
    for($i=0; $i<sizeof($a); $i++){
        if(array_key_exists($i,$b)){
            $b[$i]['id']=$b[$i]['a_id'];
            unset($b[$i]['a_id']);
        }
        $result[] = array_merge($a[$i], array_key_exists($i,$b)?$b[$i]:array());
    }
    
    print_r($result);
    

    from here https://stackoverflow.com/a/68579169/10150897


  2. There will be a few ways to do this, but I’ll demonstrate the use of references. Ideally, you should not perform nested loops or use more than two loops.

    Code: (Demo)

    // create references for each row of the first array
    foreach ($first as &$row) {
        $ref[$row['Mark']] =& $row;
    }
    
    // prepend data to any second array data that matches the Mark value
    foreach ($second as $set) {
        if (isset($ref[$set['Mark']])) {
            $ref[$set['Mark']] = $set + $ref[$set['Mark']];
        }
    }
    
    // The first array is mutated to include desired data from the second array
    var_export($first);
    

    If you want to avoid using references and are happy to generate a third array, you can create a lookup array from the first array then merge qualifying data from the second array with the relevant lookup element when pushing into the result array. (Demo)

    $result = [];
    $lookup = array_column($first, null, 'Mark');
    foreach ($second as $set) {
        if (isset($lookup[$set['Mark']])) {
            $result[] = $set + $lookup[$set['Mark']];
        }
    }
    var_export($result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search