2011-05-30 35 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