I'd like to merge duplicated array values based on the author_id into one. My code is as follows:
public function full_array( $date_query, $minimum ) {
$data = $this->date_array_freqency( $date_query, $minimum );
$products = array();
foreach( $data as $id => $sales ) {
$products[] = array (
'author_id' => get_post_field( 'post_author', $id ),
'product_id' => $id,
'sales' => $sales
);
}
}
Multi-dimensional array looks like this:
Array
(
....
[4] => Array
(
[author_id] => 27
[product_id] => 166
[sales] => 6
)
[5] => Array
(
[author_id] => 25
[product_id] => 1056
[sales] => 6
)
[6] => Array
(
[author_id] => 27
[product_id] => 331
[sales] => 6
)
)
When there's an identical author id, I'd like to combine the array into something like this:
[4] => Array
(
[author_id] => 27
[product_id] => array( 166, 331)
[sales] => array(6, 6)
)
Any guidance is much appreciated.
$productsarray usingisset. Otherwise you will have to search the array for an entry with that author_id first. (It might make sense to have the product_id and sales be arrays in any case, so for your second item with author_id 25 that would then be[product_id] => array(331)and[sales] => array(6)- usually easier to work with such data if the format is consistent.)