如何将新值推送到php中的子数组



我有多个标题相同的帖子,但每个帖子都有一个唯一的id。我正试图用标题键和id键创建一个新数组,其中id包含一个id数组。我的计划是在这个新数组上循环显示我的帖子数据。我一直遇到两个问题:

警告:array_push((要求参数1为数组,在中给定null。。警告:试图修改中非对象的属性"ID"。。

//args
$args = array(  
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1, 
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

$stack = [];
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title= get_the_title();
$id= get_the_ID();
//logic not working
if(!in_array($title, $stack)) {
array_push ($stack, array(
'title' => $title,
'ID' => array($id)
));
}
if(in_array($title, $stack)) {
array_push ($stack->ID, $id); //error here
}
} 
print_r($stack);
} 
wp_reset_postdata()
// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

$titles = [];
$ids = [];
$day_starts = [];
$day_ends = [];
$time_ends = [];
$time_starts = [];
$ods = [];
$vids =[];
$imgs = [];
$links = [];
$lss = [];
$cats = [];
while ( $the_query->have_posts() ) {
$the_query->the_post();
$title= get_the_title();
$id= get_the_ID();
$vid = 0;
$od = 0;
$ls = 0;
$img = get_the_post_thumbnail_url();
$link = get_the_permalink();
$cat = get_the_terms($id, 'event-categories');

$post_options = get_post_meta( get_the_ID(), 'post_options')[0];
print_r($post_options);
if(isset($post_options['on_demand'])){ $od = 1;}
if(!isset($post_options['on_demand']) && $post_options['featured_video'] !== ''){ $ls = 1;}
if($post_options['featured_video'] !== ''){ $vid = 1;}

$startDate = /* str_replace('-', '',  */get_post_meta( get_the_ID(), '_event_start_date')[0];
$startTime = /* str_replace(':', '',  */get_post_meta( get_the_ID(), '_event_start_time')[0];
$endDate = /* str_replace('-', '',  */get_post_meta( get_the_ID(), '_event_end_date')[0];
$endTime = /* str_replace(':', '',  */get_post_meta( get_the_ID(), '_event_end_time')[0];

array_push ($titles,$title);
array_push ($ids, $id);
array_push ($day_starts, $startDate);
array_push ($day_ends, $endDate);
array_push ($time_starts, $startTime);
array_push ($time_ends, $endTime); 
array_push ($ods, $od); 
array_push ($vids, $vid);
array_push ($lss, $ls); 
array_push ($imgs, $img);
array_push ($links, $link); 
array_push ($cats, $cat[0]->name); 
} 
} 
wp_reset_postdata();
$unique = array_unique($titles);
if($unique){
foreach($unique as $name){
$data[] = array_merge(['title' => $name ], array(
'ID' => array_merge(array_intersect_key($ids, array_intersect($titles, [$name]))),
'vid' => array_merge(array_intersect_key($vids, array_intersect($titles, [$name]))),
'od' => array_merge(array_intersect_key($ods, array_intersect($titles, [$name]))),
'start_day' => array_merge(array_intersect_key($day_starts, array_intersect($titles, [$name]))),
'end_day' => array_merge(array_intersect_key($day_starts, array_intersect($titles, [$name]))),
'start_time' => array_merge(array_intersect_key($time_starts, array_intersect($titles, [$name]))),
'end_time' => array_merge(array_intersect_key($time_ends, array_intersect($titles, [$name]))),
'link' => array_merge(array_intersect_key($links, array_intersect($titles, [$name]))),
'media' => array_merge(array_intersect_key($imgs, array_intersect($titles, [$name]))),
'ls' => array_merge(array_intersect_key($lss, array_intersect($titles, [$name]))),
'cat' => array_merge(array_intersect_key($cats, array_intersect($titles, [$name])))
));
} 
}
?>

最新更新