【发布时间】:2021-09-10 08:04:18
【问题描述】:
我找到了有关从 foreach 中的数组中删除单个元素的建议,但在尝试从数组中删除数组时无法使其正常工作。这是我正在使用的:
<?php
$all_posts = array('1', '2', '3', '4', '5'); // example array of WordPress post IDs from a get_posts function
foreach ($all_posts as $op_post) {
$op_title = get_the_title($op_post);
$same_title_posts = get_posts([ // get posts with same title
'title' => $op_title,
'post__not_in' => array($op_post),
'posts_per_page' => -1,
'fields' => 'ids',
]);
// example contents of $same_title_posts - array('1', '2')
foreach($same_title_posts as $same_title_post) {
// function
}
// before $all_posts loops again, need to remove contents of $same_title_posts array from $all_posts array
}
?>
【问题讨论】:
标签: php arrays wordpress foreach