问题描述
|
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循环中执行此操作,则此方法在我的解决方案中不起作用,请使用所有答复和良好的代码。:它运行循环并将所有结果推入数组。
<?php query_posts(\'post_type=therapeut\');
$therapeAr = array(); ?>
<?php while (have_posts()) : the_post(); ?>
<?php $therapie = get_post_meta($post->ID,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>\';
} ?>
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)