PHP 合并数组并删除双精度值 [英] PHP merge array(s) and delete double values

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

问题描述

WP 输出一个数组:

$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 ) 

如何将这些数组合并为一个并删除所有确切的双名?

结果是这样的:

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

[已解决] 问题是,如果您在 while 循环中执行此操作,它在我的解决方案中不起作用,适用于所有回复和好的代码.:它运行循环并将每个结果推送到数组中.

[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));

或更有效的替代(感谢erisco的评论)::>

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

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

如果 $therapie 的键是字符串,您可以删除 array_unique.

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

或者,如果您想避免call_user_func_array,您可以研究扁平化多维数组的各种不同方法.这里有一些(一个两个) 已经在 SO 上的好问题,详细说明了几种不同的方法所以.

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.

我还应该注意,这仅在 $therapie 只是一个二维数组时才有效除非您不想将其完全展平.如果 $therapie 超过 2 个维度并且您想将其展平为 1 个维度,请查看我上面链接的问题.

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.

相关文档条目:

array_flip
array_keys
array_merge
array_unique
call_user_func_array

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

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