PHP 合并数组并删除双精度值

发布于 2024-11-10 12:40:06 字数 1621 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雨后咖啡店 2024-11-17 12:40:06

以下应该可以解决问题。

$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,您可以研究展平多维数组的各种不同方法。这里有一些(一个,两个)已经很好的问题,详细介绍了几种不同的方法所以。

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

相关文档条目:

array_flip
array_keys
array_merge
array_unique
call_user_func_array

The following should do the trick.

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

or the more efficient alternative (thanks to erisco's comment):

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

If $therapie's keys are strings you can drop array_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

以歌曲疗慰 2024-11-17 12:40:06

听起来您生成的数组的键无关紧要。如果是这种情况,您可以执行简单的合并,然后确定合并。 net/array_unique" rel="nofollow">具有内置 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);

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:

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

edit: an example:

// 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);
暗地喜欢 2024-11-17 12:40:06

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

PHP's array_unique() will remove duplicate values from an array.

无法言说的痛 2024-11-17 12:40:06
$tester = array();
foreach($therapie as $thera) {
   array_push($tester, $thera);
}
$result = array_unique($tester);
print_r($result);
$tester = array();
foreach($therapie as $thera) {
   array_push($tester, $thera);
}
$result = array_unique($tester);
print_r($result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文