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>';
} ?>                 

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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 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.
sorry, small update in the hurry i've coped it wrong, is right now.
@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_dumping $therapie instead.
|
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);

5 Comments

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 my answer should be what you're looking for.
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).
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...
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.
2

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

Comments

0
$tester = array();
foreach($therapie as $thera) {
   array_push($tester, $thera);
}
$result = array_unique($tester);
print_r($result);

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.