0

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.

5
  • Please post the code you have tried Commented May 20, 2019 at 9:48
  • Why is phrases_relevant value 123 missing in your output? Your output example is very incomplete. Could you complete it for the given input? Commented May 20, 2019 at 9:49
  • it is onlny example, value phrases does not matter Commented May 20, 2019 at 9:53
  • Examples always matter in questions, but I think I know what you want. You want to group the array entries on searches_id. So the columns name, id, email and phrases_relevant can all vary when the searches_id is the same? Or does only the phrases_relevant vary, as in your example? Commented May 20, 2019 at 9:54
  • if we will group searches id, email will be same everywhere. Name it is always first element phrases_relevant, so it connection. Commented May 20, 2019 at 9:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Using a ternary operator instead of the far more appropriate if/else makes this code almost impossible to understand.
0

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',
      ),
    ),
  ),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.