【问题标题】:PHP merge array(s) and delete double valuesPHP 合并数组并删除双精度值
【发布时间】:2011-09-05 00:34:27
【问题描述】:

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 循环中执行此操作,它将无法在我的解决方案中工作,对于所有回复和好的代码都是 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>';
} ?>                 

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    以下应该可以解决问题。

    $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,您可以研究各种不同的展平多维数组的方法。这里有一些(onetwo)关于 SO 的好问题,详细说明了这样做的几种不同方法。

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

    相关文档条目:

    array_flip
    array_keys
    array_merge
    array_unique
    call_user_func_array

    【讨论】:

    • Gumbo 给我的建议:array_unique()array_keys(array_flip()) 都完成了同样的事情,只是它们的时间复杂度分别为 O(nlogn)O(n)
    • ty,这段代码可能会工作,但遗憾的是我无法让它工作。我想这与持有 3 个不同数组的变量 $therapie 有关。作为记录,我已经尝试了上面所有的代码示例。
    • @Rob 编辑您的答案以包括 (使用上述方法)print_r 之前和之后的 $therapie,以及 EXACT 运行它的代码。
    • 抱歉,小更新匆匆忙忙搞错了,马上更新。
    • @Rob 在不知道所有这些函数在做什么的情况下,很难判断发生了什么。我最好的猜测是 $therapieNULL 或一个空字符串。改用var_dumping $therapie
    【解决方案2】:

    听起来您正在生成的数组的键无关紧要。如果是这种情况,您可以做一个简单的merge,然后使用内置 PHP 函数确定 unique

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

    【讨论】:

    • ty, array_merge($therapie);给了我想要合并的相同输出。 $therapie 也加载了不同的动态值。我可以合并所有 $therapie 数组而不指定它们吗?
    • @Rob 我的答案应该是你要找的。​​span>
    • 您不能立即合并输出。想想你的输出给你什么(即数组的数组)。就循环它们而言,您走在正确的道路上,您只需要构建一个包含所有存在值的数组(我建议在其中使用 array_merge),然后找出任何唯一值(因此是 array_unique)。
    • 您能举个例子(代码)吗?我想我明白你的意思和你给出的代码。如果我是对的,变量 $therapie 包含 3 个具有不同整数的数组,但我无法让它合并它们......
    • 谢谢。我试图运行代码,但我没有从 print_r 得到任何输出。不知道怎么回事。可能是wp循环引起了问题吗?我在操作中发布了完整的代码。
    【解决方案3】:

    PHP 的array_unique() 将从数组中删除重复值。

    【讨论】:

      【解决方案4】:
      $tester = array();
      foreach($therapie as $thera) {
         array_push($tester, $thera);
      }
      $result = array_unique($tester);
      print_r($result);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        • 1970-01-01
        • 2013-02-24
        • 2017-06-08
        相关资源
        最近更新 更多