0

I have two arrays from a different database, now I have to print it's in a table as a combined report. those arrays are

array:3 [▼
  0 => array:6 [▼
    "count_id" => 5
    "count_name" => "NE 28/1"
    "order_qty" => 75100
    "balance_qty" => 75100
    "stock_qty" => "0"
    "order_req" => 75100
  ]
  1 => array:6 [▼
    "count_id" => 13
    "count_name" => "NE 20/1 KH"
    "order_qty" => 2200
    "balance_qty" => 2200
    "stock_qty" => "48400"
    "order_req" => 0.0
  ]
]

and the second array

array:2 [▼
  0 => array:6 [▼
    "count_id" => 13
    "count_name" => "NE 20/1 KH"
    "order_qty" => 2200
    "balance_qty" => 2200
    "stock_qty" => "45420.85"
    "order_req" => 0.0
 ]
 1 => array:6 [▼
   "count_id" => 24
   "count_name" => "NE 24/1 KH"
   "order_qty" => 5150
   "balance_qty" => 5150
   "stock_qty" => "45420.85"
   "order_req" => 0.0
 ]
]

Now I need the common "count_name" Like "NE 20/1 KH" value will sum and print only once. Something Like...

array:3 [▼
  0 => array:6 [▼
    "count_id" => 5
    "count_name" => "NE 28/1"
    "order_qty" => 75100
    "balance_qty" => 75100
    "stock_qty" => "0"
    "order_req" => 75100
  ]
  1 => array:6 [▼
    "count_id" => 13
    "count_name" => "NE 20/1 KH"
    "order_qty" => 4400
    "balance_qty" => 4400
    "stock_qty" => "96800"
    "order_req" => 0.0
  ]
  2 => array:6 [▼
    "count_id" => 24
    "count_name" => "NE 24/1 KH"
    "order_qty" => 5150
    "balance_qty" => 5150
    "stock_qty" => "45420.85"
    "order_req" => 0.0
  ]
]

I try to do it by two foreach loop, but it's didn't work. it's print every data multiple time. so is there anyone to help me, please.

3
  • Union of the 2 queries should be enough. No need of PHP stuff. Commented Feb 12, 2020 at 14:07
  • 1
    Please always show what you tried, even if it was not successful. Commented Feb 12, 2020 at 14:11
  • Please share what you had tried to achieve it. community is not here to write down code for you. Commented Feb 12, 2020 at 14:13

2 Answers 2

1

It can be done but probably just easier to combine them via the database query in the first place.

// Your arrays of data
$data = [$array1, $array2];

// The fields you want to sum
$sumFields = ['order_qty', 'balance_qty', 'stock_qty', 'order_req'];

// The final array
$combined = [];

foreach ($data as $arr) {
    foreach ($arr as $item) {
        $key = $item['count_id'];
        if (!isset($combined[$key])) {
            $combined[$key] = $item;
        } else {
            foreach ($sumFields as $field) {
                $combined[$key][$field] += $item[$field];
            }
        }
    }
}

// Print result
print_r($combined);
Sign up to request clarification or add additional context in comments.

Comments

0

You have to loop through them. This code should work

foreach($array1 as $elem1){
    $found = false;
    foreach($array2 as $elem2){
        if($elem1->count_id = $elem2->count_id){
            $found=true
            //Add elements from elem1 to elem2
            break;
        }
    }
    if(!$found){
        array_push($array2, $elem1)        
    }
}

I hope it helps.

4 Comments

@YagnikDetroja can you share the result you get?
it's just pushing one new element in whole array. he didn't want to combine array. wanted to sum of all the value of array element which match in main array.
the code just pushes one new element if it was not found in the first array as described by op in the example. if the element is found then it should go to the //add elements part. See that 2 first arrays has 2 elements but desired one has 3, one combined and one pushed
@Gamopo thankyou to try to help me :) I try this code, but its returns only common data. it's already solved by another answer. thank you for your time... :)

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.