-2

I have an array that looks like the following:

$myArray = 
[
  ['email' => 'email1@email.com', 'value' => '123'],
  ['email' => 'email1@email.com', 'value' => '456'],
  ['email' => 'email1@email.com', 'value' => '789'],
  ['email' => 'email2@email2.com', 'value' => '123'],
  ['email' => 'email2@email2.com', 'value' => '456'],
  ...
]

What is a good way to combine the values according to unique keys (email) such that the previous array would end up like so:

[
  'email@email.com' => ['123', '456'],
  'email2@email2.com' => ['123', '456', '789'],
  ...
]

I can think of solutions, but they all seem quite inefficient :( Here is one for example:

$resultArray = array();
foreach ($myArray as $info) {
    if (!isset($resultArray[$info['email']])) {
        $resultArray[$info['email']] = array();
    }
    array_push($resultArray[$info['email']], $info['value']);
}

This doesn't sit well with me for some reason.

QUESTION

Are there better solutions?

-- Updated my Blatant Mistake - Check new (1st) array :/ --

karns
  • 5,391
  • 8
  • 35
  • 57
  • 1
    You can't have the same key twice! – Rizier123 Jun 09 '15 at 12:26
  • Right, try `var_dump()` there will be only 2 keys. – AbraCadaver Jun 09 '15 at 12:26
  • How are you generating `$myArray`? – Sougata Bose Jun 09 '15 at 12:27
  • I'm not sure what you're getting at, @Rizier123. I don't think I do... nor do I want to... – karns Jun 09 '15 at 12:27
  • My point is to keep only the unique keys, but aggregate their values. – karns Jun 09 '15 at 12:28
  • @b0s3 - It is actually generated in a previous step from a SQL result. However, I am not majorly concerned about that. – karns Jun 09 '15 at 12:28
  • 3
    @karns If you have 2 elements with the same key your array will only have 1 element – Rizier123 Jun 09 '15 at 12:30
  • 1
    I agree with @Rizier123... It is possible same key in multidimensional array only – Deep Kakkar Jun 09 '15 at 12:31
  • The above array is not possible http://stackoverflow.com/questions/30636179/how-does-php-index-associative-arrays/30636430#30636430 – Narendrasingh Sisodia Jun 09 '15 at 12:32
  • @Rizier123 - Please read through my question more thoroughly and let me know how I could make it clearer. I know that I can't have duplicate keys. – karns Jun 09 '15 at 12:32
  • @Uchiha - It is indeed possible. Notice how I don't have keys that collide. I have "email..." and "email2..." – karns Jun 09 '15 at 12:33
  • Oh! My apologies everyone, the first array is incorrect * I was thinking you were talking about the answer.. Let me update the question :/ – karns Jun 09 '15 at 12:35
  • 1
    That's impossible [Check This](https://eval.in/378331) – Narendrasingh Sisodia Jun 09 '15 at 12:35
  • 1
    @karns *I know that I can't have duplicate keys* There is your first problem. How do you want to combine all elements with the same key if they can't exist ? – Rizier123 Jun 09 '15 at 12:35
  • @karns *the first array is incorrect* Are you serious right now?! – Rizier123 Jun 09 '15 at 12:44
  • @Rizier123 - I am. Am I screwed in the head? It must be too early for me. What is the issue with it if they are nested arrays now? – karns Jun 09 '15 at 12:46
  • @karns Now nothing is wrong anymore... And I look like a fool which fell for the trap. (Your code is good and works, I don't think you can write it much better than that; Besides the typo with the dollar sign in your index) – Rizier123 Jun 09 '15 at 12:47
  • 1
    @Rizier123 - Thank you, I appreciate your patience with my ingorance :P I hope at the very least people got entertainment out of this. What do people think - should I remove this question? – karns Jun 09 '15 at 12:50
  • 1
    @karns It's up to you, but I think you can go with this code which you already have (if you fix the dollar sign typo). – Rizier123 Jun 09 '15 at 12:54
  • `foreach ($myArray as $item) { $newArray[$item['email']][] = $item['value']; }`. [Example here](http://ideone.com/v4WE0g). – Phylogenesis Jun 09 '15 at 12:55

2 Answers2

2

All your checks and uses of array_push() can be replaced by the empty array indexer. This greatly simplifies your loop as follows:

foreach ($myArray as $item) {
    $result[$item['email']][] = $item['value'];
}

An example of this is here.

Phylogenesis
  • 7,775
  • 19
  • 27
1

You can simply use foreach as

$myArray = 
[
  ['email' => 'email1@email.com', 'value' => '123'],
  ['email' => 'email1@email.com', 'value' => '456'],
  ['email' => 'email1@email.com', 'value' => '789'],
  ['email' => 'email2@email2.com', 'value' => '123'],
  ['email' => 'email2@email2.com', 'value' => '456'],
];
$result = array();
foreach ($myArray as $key => &$value) {
  if(isset($result[$value['email']])){
      array_push($result[$value['email']],$value['value']);
  }else{
      $result[$value['email']] = array($value['value']);
  }
}

DEMO

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54