1

I have an array which I populate like this:

array_push($comboUserPosts, 
    array(
        'link'=> get_permalink(),
        'dates'=> $value,
        'title'=> get_the_title()
    )
);

Then when I finish all my loop, I do:

array_unique($comboUserPosts);

But it still gives me duplicates. If I do:

echo '<pre>' . var_export($comboUserPosts, true) . '</pre>';

This is what I get:

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

But by looking at that, I only have 2 unique links and title, I should only display those two links+title when I do:

foreach ($comboUserPosts as $value) { ?>
    <h2><a href="<?php echo $value['link']; ?>"><?php echo $value['title']; ?></a>
<?php }
rob.m
  • 9,843
  • 19
  • 73
  • 162

4 Answers4

3

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',
  ),
)
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • You could also just do: `$link_titles = array_column($data, 'title', 'link');` then loop through key and values for your links and titles. – Progrock Jun 07 '18 at 11:45
2

You just need to remove elements dates first then use array_unique

foreach($arr as $k=> &$v){
  unset($v['dates']);
}
print_r(array_unique($arr, SORT_REGULAR));

Live Demo

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • ok NP but you said `@C2486 I don't care about the dates, I only need the two unique links and title.` so I came up with this solution – Niklesh Raut Jun 07 '18 at 11:33
  • You're right, I meant I don't care at this point in the code as I thought you were going to come out with a solution ignoring it but I didn't think about removing them, thanks tho, I'll upvote as it does indeed answer the question as per what I've asked :) – rob.m Jun 07 '18 at 11:36
1

You can write code like this

<?php 
$ar = array (
  0 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => '1920',
    'title' => 'test values users',
  ),
  1 => 
  array (
    'link' => 'https://example.com/test/test-values-users/',
    'dates' => ' 1954',
    'title' => 'test values users',
  ),
  2 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1600',
    'title' => 'provo filter',
  ),
  3 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1450',
    'title' => 'provo filter',
  ),
  4 => 
  array (
    'link' => 'https://example.com/test/provo-filter/',
    'dates' => '1330',
    'title' => 'provo filter',
  ),
);
function unique_multidimensional_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; 
        } 
$unique = unique_multidimensional_array($ar, 'link');
echo "<pre>";
print_r($unique);

?>
Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • will test but remember I need the title too as per mm looks good but careful as I need the title too `

    `

    – rob.m Jun 07 '18 at 11:25
  • You can change key as array if you want to test with multiple keys. Generally, Title match with url so must be unique so no need to test title – Bhumi Shah Jun 07 '18 at 11:27
  • ok thanks, I've upvoted but I am going to accept the other answer as it is much shorter unless you see an issue with it – rob.m Jun 07 '18 at 11:29
1

you can use this method

array_unique($associativeArray,SORT_REGULAR);

Define an associative array $associativeArray with name and age keys.

  $associativeArray[] = array("name" => "Yogesh Singh","no"=>4);
  $associativeArray[] = array("name" => "Sonarika Singh","no"=>4);
pankaj
  • 1
  • 17
  • 36