Remove Duplicated Elements of Associative Array in PHP

How to remove duplicate values from an associative array based on a specific value?

A quick way using array_reduce would look like:

$unique = array_reduce($subject, function($final, $article){
static $seen = array();
if ( ! array_key_exists($article['fk_article_id'], $seen)) {
$seen[$article['fk_article_id']] = NULL;
$final[] = $article;
}
return $final;
});

Try a working example.


Edit: Seems you don't want to work with the aggregated results. Here are two other thoughts:

Filtering in PHP

$seen = array();
while ($row = pg_fetch_assoc($authors_result)) {
// Skip this row if we have already seen its article id
if (array_key_exists($row['fk_article_id'], $seen)) {
continue;
}
// Note that we have seen this article
$seen[$row['fk_article_id']] = NULL;
// Do whatever with your row
var_dump($row);
}

Filtering in the DB

The idea here is to change the query being executed so that the repeated article ids do not appear in the result set. How this is done will depend on the query that you're already using.

How to make a unique associative array?

The easiest I could come up with is to index the array by the link (using array_column()) and then just extract the values...

$comboUserPosts = array_values(array_column($comboUserPosts, null, 'link'));
echo var_export($comboUserPosts, true);

With your test data above it gives...

array (
0 =>
array (
'link' => 'https://example.com/test/test-values-users/',
'dates' => ' 1954',
'title' => 'test values users',
),
1 =>
array (
'link' => 'https://example.com/test/provo-filter/',
'dates' => '1330',
'title' => 'provo filter',
),
)

How to remove duplicate values from a multi-dimensional array in PHP

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));

How to remove duplicate values from an array in PHP

Use array_unique().

Example:

$array = array(1, 2, 2, 3);
$array = array_unique($array); // Array is now (1, 2, 3)

How to remove duplicate entries from associative array in php

Try something like this, not so pretty as array_ one liners, but still:

$existing_aff_ids = array();
$unique = array();
foreach ($affiliate as $aff) {
if (!isset($existing_aff_ids[$aff['affiliate_id']])) {
$unique[] = $aff;
$existing_aff_ids[$aff['affiliate_id']] = 1;
}
}

Remove elements from associative array with two matching sub values

You can use SORT_REGULAR option More doc is HERE about array_uniqy()

    <?php

$result = array(
0=>array(0=>'2018-03-03 07:43:15',1=>'TicketID_25500'),
1=>array(0=>'2018-03-03 08:00:00',1=>'TicketID_25500'),
2=>array(0=>'2018-03-03 08:00:00',1=>'Ticket_Reply_25500'),
);

$details = unique_multidim_array($result ,'1');
print_r($details);
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();

foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}

O/P is:

Array
(
[0] => Array
(
[0] => 2018-03-03 07:43:15
[1] => TicketID_25500
)

[2] => Array
(
[0] => 2018-03-03 08:00:00
[1] => Ticket_Reply_25500
)

)

Edited:

`unique_multidim_array($result ,'1');` 

This Function is passing a two params. one is array and another one is the key for the Unique values.

In explanation of checking the array key value is already exist or not

if(!in_array($val[$key], $key_array))

If that Value and key not in array going to return the array else it ejects like:

    $temp_array[$i] = $val;
return $temp_array;

You can change the Key for your convenience like 'Numeric key' or 'String key'
like unique_multidim_array($result ,'a'); or unique_multidim_array($result ,'b');.



Related Topics



Leave a reply



Submit