0

Basically I have two arrays.

enter image description here

From index 0 to 9 are my first, and 10 to 20 is the second. What I want is merge or put the array in the first one if they have the same users_mws_id but what I get is the one in the picture. What I tried is

                foreach($newArr as $arr){
                    $res = DB::table('admin_case_info')
                    ->where('users_mws_id',$arr->users_mws_id)
                    ->where(function($q){
                        $q->orWhereIn('issue_type',['damaged2','lost2 ','disposed_of_dse','disposed_of_inb'])
                            ->orWhereIn('case_type',['ORDER_NEVER_RETURNED','ORDER_RETURNED_WITH_INCORRECT_FNSKU','OTHER_CONCESSIONS_GOODWILL','INBOUND_SHIPMENT'])
                            ->orWhereNull('case_type')
                            ->orWhereNull('issue_type')
                            ->whereNotIn('case_status',['CLOSED','ACI']);
                            
                    })
                    ->select('users_mws_id')
                    ->selectRaw('sum(pending) as pending,sum(approved) as approved,sum(rejected) as rejected',)->get()->toArray();
        
                    foreach($res as $d){
                        if($arr->users_mws_id === $d->users_mws_id){
                            array_push($newArr,$d);
                        }
                    }
                }
                dd($newArr);

$newArr is my main Array. Can someone tell me what I missed or need to do? Thanks.

EDIT. $newArr is from this query

            $users = DB::query()->select(
                'mws_name',
                DB::raw('MAX(created_at) as created_at'),
                'updated_at',
                'users_mws_id'
            )
                ->fromSub(function ($query) {
                    $query->select([
                        'um.mws_name',
                        'ac.users_mws_id',
                        'ac.case_type',
                        'ac.created_at',
                        'ac.pending',
                        'ac.approved',
                        'ac.rejected',
                        'ac.updated_at',
                        'ac.issue_type',
                        'um.user_type',
                    ])->from('admin_case_info as ac')
                    ->join('users_mws as um', 'ac.users_mws_id', '=', 'um.id')
                    ->join('users','users.id','=','um.user_id')
                    ->whereNotIn('users.status',['Dumped','Dumped2'])
                    ->where('users.user_type','user')
                    ->where('um.user_type','user')
                    ->where(function($q){
                        $q->orWhereIn('ac.issue_type',['damaged2','lost2 ','disposed_of_dse','disposed_of_inb'])
                            ->orWhereIn('ac.case_type',['ORDER_NEVER_RETURNED','ORDER_RETURNED_WITH_INCORRECT_FNSKU','OTHER_CONCESSIONS_GOODWILL','INBOUND_SHIPMENT'])

                            ->whereNotIn('ac.case_status',['CLOSED','ACI']);    
                    })
                    ->whereNull('ac.deleted_at');
                }, 'acum')
                ->groupBy('acum.users_mws_id')
                ->orderByRaw('MAX(acum.created_at) asc')
                ->limit(10)
                ->get();
                // dd($users);
                // ->toSql();
                
                $newArr = [];
                foreach ($users as $key => $u){
                    $newArr[] = $u;
                }
6
  • What is $newArr? Commented Apr 20, 2021 at 5:15
  • $newArr is the array from 0-9 from the picture. @BABAKASHRAFI Commented Apr 20, 2021 at 5:17
  • 1
    Your graphic does a poor job of representing your exact desired output from the graphic input. Please provide a minimal reproducible example that does not offer text in graphics. Array you simply trying to array_chunk() your data? "PHP array merge with conditions" is a very vague title that would apply to hundreds of pages here. Can you improve your question title? Commented Apr 20, 2021 at 5:51
  • use UNION to merge it,. Commented Apr 20, 2021 at 5:52
  • Are you asking how to groupby and sum your data? If you are able to collect all of the targeted data and you just need to restructure it, then show us your data as var_export() instead of dd(). Commented Apr 20, 2021 at 6:05

2 Answers 2

1

Below is what you were after however, This would cause a massive amount of queries.

You should be doing it as a relation, also thats if the newArr is actually a collection, if not make it one.

$stuff = collect($newArr)->map(function($item) {
    $res = DB::table('admin_case_info')
        ->where('users_mws_id', $item->users_mws_id)
        ->where(function($q){
            $q->orWhereIn('issue_type',['damaged2','lost2 ','disposed_of_dse','disposed_of_inb'])
                ->orWhereIn('case_type',['ORDER_NEVER_RETURNED','ORDER_RETURNED_WITH_INCORRECT_FNSKU','OTHER_CONCESSIONS_GOODWILL','INBOUND_SHIPMENT'])
                ->orWhereNull('case_type')
                ->orWhereNull('issue_type')
                ->whereNotIn('case_status',['CLOSED','ACI']);
                
        })
        ->select('users_mws_id')
        ->selectRaw('sum(pending) as pending,sum(approved) as approved,sum(rejected) as rejected',)->get()->toArray();
        
        return array_merge($item, $res);
});

Please show us the model of your $newArr, This is a simple relation. we can return it all in one go.

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

7 Comments

im getting Call to a member function map() on array
@KIKOSoftware you get used to it spending a whole day looking at questions on stack overflow.
@draw134 updated answer, this is because it was actually an array not a collection.
okay. but now it returns Call to undefined method stdClass::push()
@draw134 then array merge or combine or create a new array. - updated answer
|
0

You can group the result of newArr after your codes based on users_mws_id

collect($newArr)->groupBy('users_mws_id');

8 Comments

do i need to put this inside the loop right sir?
No. after the loop
in the first loop? or in the second one with the condition
ya. tried it but it only outputs the original values of $newArr
It'll group your result based on your desired key. refer to this
|

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.