Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining multidimensional arrays in php

What I am trying

I have 2 arrays as shown below:

Array 1:

Array
(
 [38] => Array
    (
        [0] => Array
            (
                [code] => ACC
                [amount] => 50
                [emp_number] => 38
            )

        [1] => Array
            (
                [code] => CAR
                [amount] => 60
                [emp_number] => 38
            )

    )

[22] => Array
    (
        [0] => Array
            (
                [code] => ACC
                [amount] => 110
                [emp_number] => 22
            )

        [1] => Array
            (
                [code] => AIR
                [amount] => 260
                [emp_number] => 22
            )

        [2] => Array
            (
                [code] => CAP
                [amount] => 205
                [emp_number] => 22
            )
      )

 )

Array2:

 Array
(
[0] => Array
    (
        [code] => ACC
    )

[1] => Array
    (
        [code] => AIR
    )

[2] => Array
    (
        [code] => ARC
    )

[3] => Array
    (
        [code] => ATV
    )

[4] => Array
    (
        [code] => CAP
    )

[5] => Array
    (
        [code] => CAR
    )

)

What I Want

Final Array:

   Array
(
 [38] => Array
    (
        [0] => Array
            (
                [code] => ACC
                [amount] => 50
                [emp_number] => 38
            )

        [1] => Array
            (
                [code] => AIR
                [amount] => ''
                [emp_number] => 38
            )
        [2] => Array
            (
                [code] => ARC
                [amount] => ''
                [emp_number] => 38
            )

        [3] => Array
            (
                [code] => ATV
                [amount] => ''
                [emp_number] => 38
            )

        [4] => Array
            (
                [code] => CAP
                [amount] => ''
                [emp_number] => 38
            )

        [5] => Array
            (
                [code] => CAR
                [amount] => 60
                [emp_number] => 38
            )

    )

[22] => Array
    (
        [0] => Array
            (
                [code] => ACC
                [amount] => 110
                [emp_number] => 22
            )

        [1] => Array
            (
                [code] => AIR
                [amount] => 260
                [emp_number] => 22
            )
        [2] => Array
            (
                [code] => ARC
                [amount] => ''
                [emp_number] => 22
            )

        [3] => Array
            (
                [code] => ATV
                [amount] => ''
                [emp_number] => 22
            )

        [4] => Array
            (
                [code] => CAP
                [amount] => 205
                [emp_number] => 22
            )

        [5] => Array
            (
                [code] => CAR
                [amount] => ''
                [emp_number] => 22
            )
      )

 )

Code

 $count = count($category);
    $exp = array();
    foreach ($expItem as $empItem) {
        $j = 0;
        foreach ($category as $cat) {
            foreach ($empItem as $emp) {
                for ($k = 0; $k < $count; $k++) {
                    if ($emp['code'] == $category['code']) {
                        $expItem[$emp['emp_number']][$k]['name'] = $category[$k]['name'];
                    } else {
                        $expItem[$emp['emp_number']][$k]['code'] = $category[$k]['code'];
                        $expItem[$emp['emp_number']][$k]['name'] = $category[$k]['name'];
                    }
                }
            }
            $j++;
        }
    }

Here $expItem is the first array and $category is the second array and I'm getting final array in $expItem itself. $emp['emp_number'] is the cell key of first array '38' and '22'. Its available in first array along with code and amount. The $category also contains value name

How can I combine the 2 arrays so that I can get the final array correctly?

EDIT: The code in first array is same as that in second array. Second array contains all codes and respective names. First array shows employee data where it shows how much amount is there for code which employee used. What I want is if employee hasn't used any code then the first array should show the corresponding code and amount as 0 in employee data. The codes are dynamically achieved so cannot hardcode them.

like image 538
Avinash Avatar asked Aug 05 '26 07:08

Avinash


1 Answers

Can you try this one? $result will have the expected array.

$result = [];

foreach($expItem as $key => $item) {

    $result[$key] = array_map(function($value) use ($key, $item){
        $value['amount'] = '';
        $value['emp_number'] = $key;

        foreach($item as $_key => $_item) {
            if($_item['code'] === $value['code']) {
                $value['amount'] = $_item['amount'];
            }
        }

        return $value;

    }, $category);

}
like image 184
codenut Avatar answered Aug 07 '26 23:08

codenut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!