wordpress - How can I push a new value to a sub array in php?

I have multiple posts with the same title but each one has a unique id. I am trying to make a new array with a title key and id key where id holds an array of the ids. My plan is to then loop over that new array to display my post data. I keep running into 2 problems:

Warning: array_push() expects parameter 1 to be array, null given in .. Warning: Attempt to modify property 'ID' of non-object in ..

//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()

Answer

Solution:

// 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])))
            ));
        } 
    }
?>

Source