在 while 循环中去除数组中的重复内容
Strip out duplicate content from an array in while loop
我正在寻找一种从数组中删除重复内容的方法。问题是这些基本上都是从 while loop
获得的不同数组
我有 2 个自定义 post 类型“学校”和“课程”,它们之间有一个 ACF 字段关系。在课程分类页面上,我展示了当前分类的课程,以及与该分类的课程相关的学校。因此,学校重复的次数与与该学校相关的课程数一样多。
所以,这是
<?php
while (have_posts()) :
the_post();
$school_items = get_field('shkola');
foreach ($school_items as $school_item) :
endforeach; ?>
<pre><?php print_r($school_item); ?></pre>
<?php endwhile; ?>
给我包含重复项的记录
WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 32)
我试过array_unique
,但我无法正确使用它,它仍然重复出现。
这与这里的情况非常相似https://support.advancedcustomfields.com/forums/topic/strip-out-duplicate-content-from-an-array/,但是那里描述的方法对我也不起作用。
如有任何帮助,我将不胜感激)
更新
来自 print_r($school_items);
的输出
Array ([0] => WP_Post Object ( [ID] => 37 ) )
Array ([0] => WP_Post Object ( [ID] => 37 ) )
Array ([0] => WP_Post Object ( [ID] => 32 ) )
你可以使用一个简单的,我在测试之前见过这个
<?php
$school_items = get_field('shkola');
if ($school_items) :
$seenIt = [];
foreach ($school_items as $school_item) :
if ( !in_array($school_item->ID, $seenIt) ) :
$title = get_the_title($school_item->ID);
?>
<div class="title">
<h2><?php echo esc_html($title); ?></h2>
</div>
<?php
$seenIt[] = $school_item->ID;
endif;
endforeach;
endif;
?>
好的,所以如果你想从原始数组中删除重复项,这也可以做到
$school_items = get_field('shkola');
if ($school_items) :
$seenIt = [];
foreach ($school_items as $key => $school_item) :
if ( in_array($school_item->ID, $seenIt) ) :
unset($school_items[$key]);
else:
$title = get_the_title($school_item->ID);
?>
<div class="title">
<h2><?php echo esc_html($title); ?></h2>
</div>
<?php
$seenIt[] = $school_item->ID;
endif;
endforeach;
endif;
我正在寻找一种从数组中删除重复内容的方法。问题是这些基本上都是从 while loop
获得的不同数组我有 2 个自定义 post 类型“学校”和“课程”,它们之间有一个 ACF 字段关系。在课程分类页面上,我展示了当前分类的课程,以及与该分类的课程相关的学校。因此,学校重复的次数与与该学校相关的课程数一样多。
所以,这是
<?php
while (have_posts()) :
the_post();
$school_items = get_field('shkola');
foreach ($school_items as $school_item) :
endforeach; ?>
<pre><?php print_r($school_item); ?></pre>
<?php endwhile; ?>
给我包含重复项的记录
WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 37)
WP_Post Object ([ID] => 32)
我试过array_unique
,但我无法正确使用它,它仍然重复出现。
这与这里的情况非常相似https://support.advancedcustomfields.com/forums/topic/strip-out-duplicate-content-from-an-array/,但是那里描述的方法对我也不起作用。
如有任何帮助,我将不胜感激)
更新
来自 print_r($school_items);
Array ([0] => WP_Post Object ( [ID] => 37 ) )
Array ([0] => WP_Post Object ( [ID] => 37 ) )
Array ([0] => WP_Post Object ( [ID] => 32 ) )
你可以使用一个简单的,我在测试之前见过这个
<?php
$school_items = get_field('shkola');
if ($school_items) :
$seenIt = [];
foreach ($school_items as $school_item) :
if ( !in_array($school_item->ID, $seenIt) ) :
$title = get_the_title($school_item->ID);
?>
<div class="title">
<h2><?php echo esc_html($title); ?></h2>
</div>
<?php
$seenIt[] = $school_item->ID;
endif;
endforeach;
endif;
?>
好的,所以如果你想从原始数组中删除重复项,这也可以做到
$school_items = get_field('shkola');
if ($school_items) :
$seenIt = [];
foreach ($school_items as $key => $school_item) :
if ( in_array($school_item->ID, $seenIt) ) :
unset($school_items[$key]);
else:
$title = get_the_title($school_item->ID);
?>
<div class="title">
<h2><?php echo esc_html($title); ?></h2>
</div>
<?php
$seenIt[] = $school_item->ID;
endif;
endforeach;
endif;