简体   繁体   中英

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.

<?php
$arr_final = array();
foreach($array1 as $key1=>$item_arr1){
    $arr_temp = array();
    foreach($array2 as $key2=>$item_arr2){
        foreach($item_arr1 as $key3=>$item_arr3){
            if($item_arr3['code'] == $item_arr2['code']){
                $arr_temp[$key2]['code'] = $item_arr3['code'];
                $arr_temp[$key2]['amount'] = $item_arr3['amount'];
            }
        }
        if(!isset($arr_temp[$key2]['code'])){
            $arr_temp[$key2]['code'] = $item_arr2['code'];
            $arr_temp[$key2]['amount'] = '';
        }
    }
    $arr_final[] = $arr_temp;
}
print_r($arr_final);
?>

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);

}

Can you try this:

<?php
$arr1 = array(38 => array(array('code'    =>  'ACC', 'amount' => 50), 
                          array('code'    =>  'CAR', 'amount' => 60)),
              22 => array(array('code'    =>  'ACC', 'amount' => 110), 
                          array('code'    =>  'AIR', 'amount' => 260),
                          array('code'    =>  'CAP', 'amount' => 205)));

$arr2 = array(array('code'  =>  'ACC'), array('code'  =>  'AIR'),
              array('code'  =>  'ARC'), array('code'  =>  'ATV'),
              array('code'  =>  'CAP'),array('code'  =>  'CAR'));
$outputArr = array();
$arrModified = array_values($arr1);
$arrayCount1 = count($arrModified);
$arrayCount2 = count($arr2);
for ($i = 0; $i < $arrayCount1; $i++) {
    for ($j = 0; $j < $arrayCount2; $j++) {
        $outputArr[$i][$j]['code'] = $arr2[$j]['code'];
        $outputArr[$i][$j]['amount'] = '';
        $tempArrCount = count($arrModified[$i]);
        for ($k = 0; $k < $tempArrCount; $k++) {
            if ($arrModified[$i][$k]['code'] == $outputArr[$i][$j]['code']) {
                $outputArr[$i][$j]['amount'] = $arrModified[$i][$k]['amount'];
                break;
            }
        }
    }
}
echo '<pre>';
print_r($outputArr);
?>

Try this :

$exp = array();
foreach ($expItems as $key => $empItem) {
    foreach ($category as $key2 => $cat) {
        foreach ($empItem as $emp) {
            if ($emp['code'] == $cat['code']) {
                $finalArray[$key][$key2] = $emp;
            }
        }
        if(!isset($finalArray[$key][$key2])){
            $finalArray[$key][] = array(
                'code' => $cat['code'],
                'amount' => '',
            );
        }
    }
}

echo '<pre>';
print_r($finalArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM