2

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.

7
  • It's somewhat unclear to me what you're asking. Can you describe in a very simple sentence what the relationship of the first and the second array is? I don't really see it. Commented Nov 26, 2015 at 9:22
  • Is it correct to say that you want to augment the first array so that all codes from the second array are added to it if they're not already in there? It also looks like the numeric keys of the first array are irrelevant? Commented Nov 26, 2015 at 9:24
  • can you show all the keys(e.g emp_number) of both arrays? whats the output when you run your code? Commented Nov 26, 2015 at 9:24
  • Which version of PHP you are using? Commented Nov 26, 2015 at 9:29
  • @AshishChoudhary: 5.4 Commented Nov 26, 2015 at 9:32

4 Answers 4

3

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

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

1 Comment

Yes it's correct, Change this line$result[] = array_map(function($value) use ($key, $item){ to $result[$key] = array_map(function($value) use ($key, $item){ to get the final output as you want.
3
<?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);
?>

Comments

2

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

Comments

1

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

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.