2011-05-30 82 views
3

WP输出数组:PHP合并数组(S)和删除双重价值

$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循环,它不会在这里工作我的解决方案,TY所有回复的和好的代码:其运行循环并推动数组中的每个结果。

<?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

下应该做的诀窍。

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

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

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

如果$therapie的键是字符串,你可以放下array_unique

或者,如果你想避免call_user_func_array,你可以看看扁平化多维数组的各种不同方法。这里有几个(one,two)好的问题已经详细说明了几种不同的方法。

我也应该注意,这只会在$therapie只是一个二维数组,除非你不想完全扁平化。如果$therapie超过2维,并且想要将其平面化为1维,请查看上面链接的问题。

相关文档的条目:

array_flip
array_keys
array_merge
array_unique
call_user_func_array

+2

Gumbo给我的建议:'array_unique()'和'array_keys(array_flip())'都完成同样的事情,除了他们有时间复杂性'O(nlogn )'和'O(n)'。 – erisco 2011-05-30 20:15:27

+0

@erisco很酷,感谢分享! – anomareh 2011-05-30 20:24:04

+0

ty,这段代码很可能会起作用,但可惜我无法让它正常工作。我想这与处理3个不同数组的变量$ therapie有关。为了记录,我已经尝试了上面的所有代码示例。 – Rob 2011-05-30 21:06:12

4

这听起来像你正在生成的数组的键是微不足道的。如果是这样的话,你可以做一个简单的merge然后确定unique那些内置了PHP函数:

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

编辑:一个例子:

// 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); 
+0

TY,array_merge($ THERAPIE);给我我想要合并的相同输出。另外$ therapie也会加载不同的动态值。我可以合并所有$ therapie数组而不指定它们吗? – Rob 2011-05-30 19:53:23

+0

@Rob我的答案应该是你在找什么。 – anomareh 2011-05-30 19:55:56

+0

你不能直接合并输出。考虑你的输出给你什么(即一个数组数组)。就循环而言,你正处在正确的道路上,你只需要构建一个包含所有值的数组(然后我推荐使用array_merge),然后找出任何唯一值(因此array_unique)。 – Owen 2011-05-30 20:00:00

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

这也给了我相同的输出,我把.. – Rob 2011-05-30 19:54:12