0

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?

4
  • 1
    What was you code with array_unique? Have you tried to use an in_array to only add items which are not there? Commented Aug 11, 2014 at 15:36
  • 1
    By looking at this code, I think you're querying and having duplicates. Maybe your query is wrong? Commented Aug 11, 2014 at 15:39
  • This seems like your solution Commented Aug 11, 2014 at 15:39
  • The query should be right, because i need the value from all of my posts. Some of them are duplicate - them I need to get rid of. Commented Aug 11, 2014 at 15:45

3 Answers 3

3

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];
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried the first solution myself without luck. The second solution doesn't do the job either.
Can you please provide the results you get for print_r($meta);?
Still returns duplicate values.
Either of these literally cannot return duplicate values mate.
Tested both of these here: sandbox.onlinephpfunctions.com/code/…
|
0

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

1 Comment

@Karsten I added two different sample code for you incl live demo
0

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

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.