I have an array like this
[
1 => [
'name' => 123,
'id' => 105740727,
'email' => 'fghfhfh',
'phrases_relevant' => 123,
'searches_id' => 105740727,
],
2 => [
'name' => 'porshe',
'id' => 105713889,
'email' => 'fghfghf',
'phrases_relevant' => 'porshe',
'searches_id' => 105713889,
],
3 => [
'name' => 'porshe',
'id' => 105713889,
'email' => 'fghfghf',
'phrases_relevant' => 'merce',
'searches_id' => 105713889,
],
]
I need group this group via value. Output array should looks like below. dimension second and third has same searches_id
[0] => Array
(
[email] => fghfghf
[projects]=>
[porshe] => [porshe, merce]
[1] => ...
My code:
foreach ($results as $key => $result) {
$testArray[]['projects'][$result['name']][] = $result['phrases_relevant'];
but this insert one phrases.
You need to sort first by searches_id
then apply loop,
function sortByOrder($a, $b)
{
return $a['searches_id'] - $b['searches_id'];
}
usort($myArray, 'sortByOrder');
foreach ($myArray as $key => $value) {
$result[$value['searches_id']]['email'] = $value['email'];
$result[$value['searches_id']]['projects'][] = $value['phrases_relevant'];
}
$result = array_values($result); // reset keys used for array generation
Working demo.