Reputation: 129
I have a function who get a array like this:
$userData[] = [
'userAddress' => $user->getAdress,
'userEmail' => $user->getEmail(),
'userPhone' => $user->getPhone(),
'userFirstname' => $user->getFirstName(),
'productCode' => $market->getProduct()->getCode(),
];
if ($currentProduct !== $market->getProductCode() or $currentProduct === 0) {
$marketProduct = $answer->getProduct();
$productData[] = [
'productCode' => $marketProduct->getCode(),
'productModel' => $marketProduct->getModel(),
'productCollection' => $marketProduct->getCollection(),
'productBrand' => $marketProduct->getBrand(),
'productFirstOnlineDate' => $answerProduct->getFirstOnlineDate(),
'usersData' => $userData,
];
}
and this fuction i call it in a foreach from a other fuction like that:
$productData = [];
foreach ($datas as $item) {
...
...
$productData[] = $this->getAlertReportData($currentProduct);
...
}
and when i do a dump alertData
out of the foreach i have a result like that for exemple
array:5 [▼
0 => array:3 [▼
"productCode" => 555
"productModel" => "productModel"
"usersData" => array:1 [▼
0 => array:3 [▼
"userAddress" => "111 ... paris"
"userEmail" => "[email protected]"
"productCode" => 555
]
]
],
2 => array:3 [▼
"productCode" => 555
"productModel" => "productModel"
"usersData" => array:1 [▼
0 => array:3 [▼
"userAddress" => "222 ... nice"
"userEmail" => "[email protected]"
"productCode" => 555
]
]
],
3 => array:3 [▼
"productCode" => 123
"productModel" => "productModel"
"usersData" => array:1 [▼
0 => array:3 [▼
"userAddress" => "123 ... monaco"
"userEmail" => "[email protected]"
"productCode" => 123
]
]
],
]
and i have a problem, I want to group all the users together in the production list if they have the same product. exemple :
array:5 [▼
0 => array:3 [▼
"productCode" => 555
"productModel" => "productModel"
"usersData" => array:2 [▼
0 => array:3 [▼
"userAddress" => "111 ... paris"
"userEmail" => "[email protected]"
"productCode" => 555
],
1 => array:3 [▼
"userAddress" => "222 ... nice"
"userEmail" => "[email protected]"
"productCode" => 555
]
]
],
2 => array:3 [▼
"productCode" => 123
"productModel" => "productModel"
"usersData" => array:1 [▼
0 => array:3 [▼
"userAddress" => "123 ... monaco"
"userEmail" => "[email protected]"
"productCode" => 123
]
]
],
]
thanks for the help.
Upvotes: 0
Views: 58
Reputation: 219
This code may help you,
function unique_array($my_array, $key) {
$result = array();
$i = 0;
$key_array = array();
foreach($my_array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$result[$i] = $val;
}
$i++;
}
return $result;
}
$getAllProducts = array([
'productCode' => 111,
'productModel' => 'productModel 1',
'productCollection' => 'productCollection 1',
'productBrand' => 'productBrand 1',
'productFirstOnlineDate' => '2020'],
['productCode' => 222,
'productModel' => 'productModel 2',
'productCollection' => 'productCollection 2',
'productBrand' => 'productBran 2',
'productFirstOnlineDate' => '2020'],
['productCode' => 111,
'productModel' => 'productModel 3',
'productCollection' => 'productCollection 3',
'productBrand' => 'productBran 3',
'productFirstOnlineDate' => '2020'],
['productCode' => 444,
'productModel' => 'productModel 4',
'productCollection' => 'productCollection 4',
'productBrand' => 'productBran 4',
'productFirstOnlineDate' => '2020'],
['productCode' => 555,
'productModel' => 'productModel 5',
'productCollection' => 'productCollection 5',
'productBrand' => 'productBran 5',
'productFirstOnlineDate' => '2020'],
);
$getAllProduct = unique_array($getAllProducts,'productCode');
foreach($getAllProduct as &$row ){
$Current_Code = $row['productCode'];
$getUsers = getUsers($Current_Code);
$row['userData'] = $getUsers;
}
function getUsers($Current_Code){
$userData = array([
'userAddress' => "111 ... paris",
'userEmail' => "[email protected]",
'productCode' => 111 ],
[
'userAddress' => "222 ... paris",
'userEmail' => "[email protected]",
'productCode' => 222],
[
'userAddress' => "222 ... paris",
'userEmail' => "[email protected]",
'productCode' => 222],
[
'userAddress' => "333 ... paris",
'userEmail' => "[email protected]",
'productCode' => 333],
[
'userAddress' => "333 ... paris",
'userEmail' => "[email protected]",
'productCode' => 333],
[
'userAddress' => "444 ... paris",
'userEmail' => "[email protected]",
'productCode' => 444],
[
'userAddress' => "555 ... paris",
'userEmail' => "[email protected]",
'productCode' => 555]
);
foreach($userData as $key => $row){
if ( $row['productCode'] != $Current_Code ) {
unset($userData[$key]);
}
}
return $userData;
}
print_r($getAllProduct);
Upvotes: 1