I have an array like this:
$r = array();
$r[] = ['name' => 'Test', 'supplierId' => 34];
$r[] = ['name' => 'Test2', 'supplierId' => 31];
$r[] = ['name' => 'Test3', 'supplierId' => 32];
$r[] = ['name' => 'Test4', 'supplierId' => 34];
$r[] = ['name' => 'Test5', 'supplierId' => 30];
$r[] = ['name' => 'Test6', 'supplierId' => 32];
Now I want to take $r and get multiple arrays back, differenced by supplierId. So I am looking for this result:
$r30 = ['name' => 'Test5', 'supplierId' => 30];
$r32 = [
['name' => 'Test3', 'supplierId' => 32],
['name' => 'Test6', 'supplierId' => 32]
];
I tried it with, but here I do not have access to $sup in array_filter.
$supplier = array(30, 31, 32, 34);
$finalArray = [];
foreach ($supplier as $sup) {
$finalArray[] = array_filter($r, function($value, $sup) {
echo $sup;
if ($value['supplierId'] == $sup) {
return true;
}
});
}//foreach
Any idea how I can solve it? Is there no native function that accomplishes this - something like create_array_based_on('supplierId');?
Thanks
$r30 = [['name' => 'Test5', 'supplierId' => 30]];be suitable?