0

I need to figure out a logic that has me confused. I have an array that looks like this.

array (
    0 => array (
        'invoice_id' => 'WUI_588'
        'email' => '[email protected]'
    ),
    1 => array (
        'invoice_id' => 'WUI_588'
        'email' => '[email protected]'
    ),
    2 => array (
        'invoice_id' => 'WUI_589'
        'email' => '[email protected]'
    ),
    3 => array (
        'invoice_id' => 'WUI_589'
        'email' => '[email protected]'
    ),
    4 => array (
        'invoice_id' => 'WUI_590'
        'email' => '[email protected]'
    ),
    5 => array (
        'invoice_id' => 'WUI_590'
        'email' => '[email protected]'
    ),
    6 => array (
        'invoice_id' => 'WUI_591'
        'email' => '[email protected]'
    ),
    7 => array (
        'invoice_id' => 'WUI_591'
        'email' => '[email protected]'
    ),
)

So if I were to print the array in table layout I would get something like this:-

INDEX | INVOICE ID | EMAIL 
  0   |   WUI_588  | [email protected]
  1   |   WUI_588  | [email protected]
  2   |   WUI_589  | [email protected]
  3   |   WUI_589  | [email protected]

What I want is something like this:-

INDEX | INVOICE ID | EMAIL 
  0   |   WUI_588  | [email protected], [email protected]
  1   |   WUI_589  | [email protected], [email protected]

....and so on. One of the attempts I made is this,

foreach ($array as $key => $value) {
  $invoiceidbucket[] = $value['invoice_id'];
  if(in_array($value['invoice_id'], $invoiceidbucket)) {
    $value['merged_email'][] = $value['email'];

    //Clearing array
    $invoiceidbucket = array;
  }
}
5
  • Show us some code that you tried or the one that you arrived at first one Commented May 2, 2017 at 12:58
  • @Thamilan, I added an unsuccessful solution. Commented May 2, 2017 at 13:08
  • 1
    Look at @Saty's answer. They bet me before answering yours :) Commented May 2, 2017 at 13:15
  • @Thamilan are you using the same logic ?? Commented May 2, 2017 at 13:17
  • @Saty, Nope. Somewhat different but using same built in methods Commented May 2, 2017 at 13:18

4 Answers 4

2

Create a desired array by using below code:-

$final_array = array();
foreach($initial_array as $arr){
  $final_array[$arr['invoice_id']]['invoice_id'] = $arr['invoice_id'];
  $final_array[$arr['invoice_id']]['email'] = (!empty($final_array[$arr['invoice_id']]['email']))?$final_array[$arr['invoice_id']]['email'].','.$arr['email'] : $arr['email'];
}
$final_array = array_values($final_array);
echo "<pre/>";print_r($final_array);

Output:- https://eval.in/785516

Note:- Now use this $final_array in your loop (foreach()) and print in desired manner

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

Comments

0

Do it like this:

$a = [];//Your array;
$retArray = []; //Output Array
foreach ($a as $key => $val) {
    $retArray['invoice_id'][] =  $retArray['email'];
}

At the end of the loop, you'll have all the invoice_ids as key with array of their email-ids.

Print anyway you like after this :)

Comments

0

My method doesn't do needless iterated overwriting. I recommend this:

Code: (Demo)

$array=[
    ['invoice_id'=>'WUI_588','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_588','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_589','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_589','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_590','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_590','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_591','email'=>'[email protected]'],
    ['invoice_id'=>'WUI_591','email'=>'[email protected]']
];
foreach($array as $row){  // iterate all rows
    if(!isset($result[$row['invoice_id']])){  // if first occurrence of invoice_id...
        $result[$row['invoice_id']]=$row;     // save the full row with invoice_id as the temporary key
    }else{                                    // if not the first occurrence of invoice_id...
        $result[$row['invoice_id']]['email'].=", {$row['email']}";  // concatenate the email value
    }
}
var_export(array_values($result));

Output:

array (
  0 => 
  array (
    'invoice_id' => 'WUI_588',
    'email' => '[email protected], [email protected]',
  ),
  1 => 
  array (
    'invoice_id' => 'WUI_589',
    'email' => '[email protected], [email protected]',
  ),
  2 => 
  array (
    'invoice_id' => 'WUI_590',
    'email' => '[email protected], [email protected]',
  ),
  3 => 
  array (
    'invoice_id' => 'WUI_591',
    'email' => '[email protected], [email protected]',
  ),
)

Comments

0

Put a check through the invoice ID column loop before starting a new row. in case of a match found, put the email in the same row, with another check which would add a comma before, in case its not the 1st email ID of the row.

May be show us how you approached it.

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.