PHP合并数组(S)和删除双重价值 [英] PHP merge array(s) and delete double values

查看:137
本文介绍了PHP合并数组(S)和删除双重价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WP输出数组:

  $ THERAPIE = get_post_meta($&后GT; ID,Therapieen',FALSE);
的print_r($ THERAPIE);//的print_r的输出
阵列([0] => Massagetherapie)
阵列([0] =>热石)
阵列([0] => Massagetherapie)

我将如何合并这些阵列之一,并删除所有确切的双名?

从而导致这样的事情:

  theArray

[0] => Massagetherapie
[1] =>热石

[解决]这个问题是,如果你这样做在一个while循环它不会在这里工作我的解决方案,TY所有回复的和良好的code:它的运行循环,并推动每一个结果的数组

 < PHP query_posts('post_type = therapeut');
$ therapeAr =阵列(); ?>
?<而PHP(have_posts()):the_post(); ?>
< PHP $ THERAPIE = get_post_meta($&后GT; ID,Therapieen',真);
如果(strpos($ THERAPIE,',')!== FALSE){//检查,如果是这样,并使阵列
$ ARR =爆炸('',$ THERAPIE);
array_push($ therapeAr,$ ARR);
}其他{
array_push($ therapeAr,$ THERAPIE);
?}>
< PHP ENDWHILE; ?>< PHP
功能array_values​​_recursive($进制){// 2:1维数组
$ LST =阵列();
的foreach(array_keys($进制)为$ K){$ V = $元[$ k]的;
如果(is_scalar($ V)){$ LST [] = $ V;
} ELSEIF(is_array($ V)){$ LST = array_merge($ LST,array_values​​_recursive($ V));}}
返回$善堂;
}功能trim_value(安培; $值)//修剪空白开始和放大器;结阵
{
$值=修剪($值);
}$ therapeAr = array_values​​_recursive($ therapeAr);
array_walk($ therapeAr,'trim_value');
$ therapeAr = array_unique($ therapeAr);的foreach($ therapeAr为$尊者){
呼应'<李><输入类型=复选框值='。$尊者。'>'$尊者。'< /输入>< /李>';
?}>


解决方案

下面应该做的伎俩。

  $扁平= array_unique(call_user_func_array('array_merge',$ THERAPIE));

或更有效率的选择的(感谢 erisco 的评论)的:

  $扁平= array_keys(array_flip(
    call_user_func_array('array_merge',$ THERAPIE)
));

如果 $ THERAPIE 键是字符串可以删除 array_unique

另外,如果你想避免 call_user_func_array ,你可以看看压扁多维数组的各种不同的方式。这里有几个(<一个href=\"http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php\">one, 2 )很好的问题已经在SO细节上这样做几种不同的方法。

我也应该注意,如果 $ THERAPIE 永远都只有一个2维数组,这只会工作的除非你不想压扁它彻底。如果 $ THERAPIE 超过2个维度,并希望将其压平成一维,看看我上面链接的问题。

相关文档的条目:

array_flip 结果
array_keys 结果
array_merge 结果
array_unique 结果
call_user_func_array

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

解决方案

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

这篇关于PHP合并数组(S)和删除双重价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆