Pekus
Pekus

Reputation: 155

Group rows of a 2d array and restructure as a multidimensional array

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.

Upvotes: 0

Views: 54

Answers (3)

mickmackusa
mickmackusa

Reputation: 47894

By using symmetrical array destructuring syntax inside a body-less loop, you can isolate only the row values that you need and push the data into groups. When the loop finishes, call array_values() to re-index the result array. Demo

foreach (
    $array as [
        'id' => $id,
        'name' => $name,
        'email' => $result[$id]['email'],
        'phrases_relevant' => $result[$id]['projects'][$name][]
    ]
);
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'email' => 'fghfhfh',
    'projects' => 
    array (
      123 => 
      array (
        0 => 123,
      ),
    ),
  ),
  1 => 
  array (
    'email' => 'fghfghf',
    'projects' => 
    array (
      'porshe' => 
      array (
        0 => 'porshe',
        1 => 'merce',
      ),
    ),
  ),
)

Upvotes: 0

Rahul
Rahul

Reputation: 18557

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.

Upvotes: 2

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

You can use foreach

$res = [];
foreach($arr as $key => $value){

array_key_exists($value['id'], $res) ?
    ($res[$value['id']]['phrases_relevant'] = $res[$value['id']]['phrases_relevant'].','.$value['phrases_relevant'])
:
($res[$value['id']] = ['email' => $value['email'],'phrases_relevant' => $value['phrases_relevant']]);
}
print_r(array_values($res))

Live Demo

Upvotes: 1

Related Questions