Basically I have two arrays.
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;
}

$newArr?$newArris the array from 0-9 from the picture. @BABAKASHRAFIarray_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?var_export()instead ofdd().