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 循环中执行此操作,则不会在这里工作我的解决方案,所有答复和良好的代码。:它运行循环并将每个结果推送到数组中。
<?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>';
} ?>
WP outputs an array:
$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 )
How would I merge these arrays to one and delete all the exact double names?
Resulting in something like this:
theArray
(
[0] => Massagetherapie
[1] => Hot stone
)
[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>';
} ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下应该可以解决问题。
或更有效的替代方案(感谢erisco的评论):
如果
$ therapie
的键是可以删除array_unique
的字符串。或者,如果您想避免 call_user_func_array,您可以研究展平多维数组的各种不同方法。这里有一些(一个,两个)已经很好的问题,详细介绍了几种不同的方法所以。
我还应该注意,只有当
$therapie
仅仅是一个二维数组时,这才有效除非你不想完全展平它。如果$therapie
超过 2 维,并且您想将其展平为 1 维,请查看我上面链接的问题。相关文档条目:
array_flip
array_keys
array_merge
array_unique
call_user_func_array
The following should do the trick.
or the more efficient alternative (thanks to erisco's comment):
If
$therapie
's keys are strings you can droparray_unique
.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.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.Relevant doc entries:
array_flip
array_keys
array_merge
array_unique
call_user_func_array
听起来您生成的数组的键无关紧要。如果是这种情况,您可以执行简单的合并,然后确定合并。 net/array_unique" rel="nofollow">具有内置 PHP 函数的独特:
编辑:示例:
It sounds like the keys of the arrays you are generating are insignificant. If that's the case, you can do a simple merge then determine the unique ones with built in PHP functions:
edit: an example:
PHP 的 array_unique() 将从数组中删除重复的值。
PHP's array_unique() will remove duplicate values from an array.