Karsten Tietje
Karsten Tietje

Reputation: 259

Remove duplicate from foreach loop

I'm trying to loop through a custom post type and display all meta values for a meta key.

I'm able to display all the values, however there are some duplicates. I want to remove those duplicates.

I have tried using array_unique without luck.

My code:

<?php
query_posts(array(
'post_type' => 'lejlighed'

 ));
 ?>
               <?php
while (have_posts()):
the_post();


$meta = get_post_meta($post->ID, 'antal_varelser');

foreach ($meta as $m) {

    echo '<option value="1">' . $m . '</option>';

}
endwhile;
?> 

The result: 2 2 3 4 3 3 4

My code with array_unique:

<?php
$the_query = new WP_Query(array(
'post_type' => 'lejlighed',
 ));
 ?>
 <?php

 while ($the_query->have_posts()):
 $the_query->the_post();
 $meta = get_post_meta($post->ID, 'antal_varelser');
 $result = array_unique($meta);

 foreach($result as $r)
 {
 echo '<option value="1">' . $m . '</option>';
 }

 endwhile; ?>

Outputs nothing.

print_r($meta) returns:

Array ( [0] => 2 ) Array ( [0] => 12 ) Array ( [0] => 4 ) Array ( [0] => 2 ) Array ( [0] => 5 ) Array ( [0] => 2 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 3 ) Array ( [0] => 3 ) 

Any suggestions?

Upvotes: 0

Views: 14889

Answers (3)

Deviation
Deviation

Reputation: 113

array_unique seems to work.

<?php
$a = array (
    '1', '2', '2', '3', '4', '5', '4'
);
echo "<pre>initial array\n";
print_r($a);
echo "\n";

$result = array_unique($a);

echo "array_unique\n";
print_r($result);
echo "\n</pre>";
?>

Produces:

initial array
Array
(
    [0] => 1
    [1] => 2
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 4
)

array_unique
Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

Can you provide additional detail? That might help us help you.

EDIT: After seeing the output of print_r, this solution should work for you:

<?php
$multi_a = array (
    array (
        '1'
    ),
    array (
        '2'
    ),
    array (
        '3'
    ),
    array (
        '4'
    ),
    array (
        '4'
    ),
    array (
        '5'
    ),
    array (
        '2'
    )
);
echo "<pre>initial multi-dimensional array\n";
print_r($multi_a);
echo "\n";

$result = array_map("unserialize", array_unique(array_map("serialize", $multi_a)));

echo "removed duplicates in multi-dimension array\n";
print_r($result);

echo "\n</pre>";
?>

Output:

initial multi-dimensional array
Array
(
    [0] => Array
        (
            [0] => 1
        )

    [1] => Array
        (
            [0] => 2
        )

    [2] => Array
        (
            [0] => 3
        )

    [3] => Array
        (
            [0] => 4
        )

    [4] => Array
        (
            [0] => 4
        )

    [5] => Array
        (
            [0] => 5
        )

    [6] => Array
        (
            [0] => 2
        )

)

removed duplicates in multi-dimension array
Array
(
    [0] => Array
        (
            [0] => 1
        )

    [1] => Array
        (
            [0] => 2
        )

    [2] => Array
        (
            [0] => 3
        )

    [3] => Array
        (
            [0] => 4
        )

    [5] => Array
        (
            [0] => 5
        )

)

Taken from https://stackoverflow.com/a/946300/720829 HTH

Upvotes: 0

nouseforname
nouseforname

Reputation: 748

You could try to use a loop and fill another array first with:

bool in_array ( $needle , array $haystack)

Then you should have only unique values in the new array, which can be used for ur loop


Edit:

For me two ways are working...

We assume this array is given:

$org = array('1', '2', '2', '3', '3', '4', '5', '6');

Simple array_unique:

// simple array unique call with sort by string
$new = array_unique($org, SORT_STRING);

// output the sorted array
var_dump($new);

Sort array in loop by using in_array:

// sort with in_array()
for ($i=0; $i < count($org); $i++) {

    if (!in_array($org[$i], $new)) {
        $new[] = $org[$i];
    }
}

// output the sorted array
var_dump($new);

Live Demo

Upvotes: 0

Tibor B.
Tibor B.

Reputation: 1690

array_unique should work.

$result = array_unique($meta);
foreach ($result as $m) {
  echo '<option value="1">' . $m[0] . '</option>';
}

If not, then...

$temp = array();
foreach ($meta as $m) {
  if (!in_array($m[0], $temp)) {
    echo '<option value="1">' . $m[0] . '</option>';
    $temp[] = $m[0];
  }
}

Upvotes: 3

Related Questions