3

WP outputs an array:

$therapie = get_post_meta($post->ID, 'Therapieen', false);
print_r($therapie);

//the output of print_r
Array ( [0] => Massagetherapie ) 
Array ( [0] => Hot stone )
Array ( [0] => Massagetherapie ) 

How would I merge these arrays to one and delete all the exact double names?

Resulting in something like this:

theArray
(
[0] => Massagetherapie 
[1] => Hot stone
)

[SOLVED] the problem was if you do this in a while loop it wont work here my solution, ty for all reply's and good code.: Its run the loop and pushes every outcome in a array.

<?php query_posts('post_type=therapeut');
$therapeAr = array(); ?>
<?php while (have_posts()) : the_post(); ?>
<?php $therapie = get_post_meta($post->ID, 'Therapieen', true);
if (strpos($therapie,',') !== false) { //check for , if so make array
$arr = explode(',', $therapie);
array_push($therapeAr, $arr);                       
} else {
array_push($therapeAr, $therapie);
} ?>
<?php endwhile; ?>

<?php           
function array_values_recursive($ary)  { //2 to 1 dim array
$lst = array();
foreach( array_keys($ary) as $k ) {

$v = $ary[$k];
if (is_scalar($v)) {

$lst[] = $v;
} elseif (is_array($v)) {

$lst = array_merge($lst,array_values_recursive($v));

}}
return $lst;
}

function trim_value(&$value) //trims whitespace begin&end array
{ 
$value = trim($value); 
}

$therapeAr = array_values_recursive($therapeAr);
array_walk($therapeAr, 'trim_value');
$therapeAr = array_unique($therapeAr);  

foreach($therapeAr as $thera) {
echo '<li><input type="checkbox" value="'.$thera.'">'.$thera.'</input></li>';
} ?>                 
Rob
  • 83
  • 1
  • 6

4 Answers4

4

It sounds like the keys of the arrays you are generating are insignificant. If that's the case, you can do a simple merge then determine the unique ones with built in PHP functions:

$array = array_merge($array1, $array2, $array3);
$unique = array_unique($array);

edit: an example:

// Emulate the result of your get_post_meta() call.
$therapie = array(
  array('Massagetherapie'),
  array('Hot stone'),
  array('Massagetherapie'),
);

$array = array();

foreach($therapie as $thera) {
  $array = array_merge($array, $thera);
}

$unique = array_unique($array);

print_r($unique);
Owen
  • 82,995
  • 21
  • 120
  • 115
  • ty, array_merge($therapie); gives me the same output I'd like to merge. Also $therapie gets loaded with dynamic values that differ. Can I merge all of the $therapie arrays without specifying them? – Rob May 30 '11 at 19:53
  • @Rob my answer should be what you're looking for. – anomareh May 30 '11 at 19:55
  • You can't merge the output straight away. Think about what your output gives you (i.e. an array of arrays). You're on the right path in terms of looping through them, you just need to build an array of all the values present (and I recommend using array_merge in that), and then find out any unique values (thus the array_unique). – Owen May 30 '11 at 20:00
  • Could you please give an example of that (code)? I think I understand what you mean and the code you're giving. If I'm right the variable $therapie holds 3 arrays with different integers, But I can't get it to work merging them... – Rob May 30 '11 at 21:02
  • Thank you. I've tried to run the code, but I didn't get any output from the print_r. Don't know what's going on. Could it be the wp loop is causing problems? I posted the complete code in the op. – Rob May 30 '11 at 22:01
4

The following should do the trick.

$flattened = array_unique(call_user_func_array('array_merge', $therapie));

or the more efficient alternative (thanks to erisco's comment):

$flattened = array_keys(array_flip(
    call_user_func_array('array_merge', $therapie)
));

If $therapie's keys are strings you can drop array_unique.

Alternatively, if you want to avoid call_user_func_array, you can look into the various different ways of flattening a multi-dimensional array. Here are a few (one, two) good questions already on SO detailing several different methods on doing so.

I should also note that this will only work if $therapie is only ever a 2 dimensional array unless you don't want to flatten it completely. If $therapie is more than 2 dimensions and you want to flatten it into 1 dimension, take a look at the questions I linked above.

Relevant doc entries:

array_flip
array_keys
array_merge
array_unique
call_user_func_array

Community
  • 1
  • 1
anomareh
  • 5,294
  • 4
  • 25
  • 22
  • 2
    Advice that Gumbo gave me: `array_unique()` and `array_keys(array_flip())` both accomplish the same thing, except they have time complexities of `O(nlogn)` and `O(n)` respectively. – erisco May 30 '11 at 20:15
  • ty, This code probably would work, but sadly I can't get it to work. I guess it has something to do with the variable $therapie holding 3 different arrays. For the record I've tried all of the code examples above. – Rob May 30 '11 at 21:06
  • @Rob edit your answer to include _(using the above method)_, a `print_r` of `$therapie` before and after, and the __EXACT__ code you're running it through. – anomareh May 30 '11 at 21:10
  • sorry, small update in the hurry i've coped it wrong, is right now. – Rob May 30 '11 at 21:34
  • @Rob It's kind of hard to tell what's going on without knowing what all those functions are doing. My best guess is that `$therapie` is `NULL` or an empty string. Try `var_dump`ing `$therapie` instead. – anomareh May 31 '11 at 00:31
  • I've solved it, it was because I was running all the code in a while loop... see op – Rob May 31 '11 at 10:55
2

PHP's array_unique() will remove duplicate values from an array.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
George Cummins
  • 28,485
  • 8
  • 71
  • 90
0
$tester = array();
foreach($therapie as $thera) {
   array_push($tester, $thera);
}
$result = array_unique($tester);
print_r($result);
cOle2
  • 4,725
  • 1
  • 24
  • 26