php foreach多个数组超时

There are two functions, one is init function to get some information from database and then transfer the account info to another function get the data from remote website, then test_function_save_data() get the correct data and check & insert them into the database.

Question is: if use the print_r($post) in test_function_save_data(), the function can work normally. but remove this print_r or replace it with sleep(), all of the data can be checked and inserted into dabase, but the current page is not redirected and blank page will be display.

so, where is wrong? how to solve it?

<?php
function test_function_get_account_init() {
    if(test_function_get_sns_account('blog')) {
        $b = 0;
        foreach (test_function_get_sns_account('blog') as $team_id => $blog_account_name) {
            $blog = test_theme_get_blog($team_id, $blog_account_name);
            if($blog && is_array($blog['posts'])){
                $b += test_function_save_data($blog['posts']);
            }
        }
        echo '<h5successfuly!</h5>';
    } else {
        echo 'not found.';
    }
}

function test_function_save_data($post_data) {
    set_time_limit(720);
    global $wpdb;
    $i = 0;
    foreach ($post_data as $post) {
        // echo '<div style="display:none;">';
        // print_r($post);
        // echo '</div>';
        if(test_function_check_post_unquine($post['social_origin_id']) && isset($post['social_origin_id'])) {
            $social_post_data = array(
                'post_title'    => $post['post_title'],
                'post_content'  => $post['post_content'],
                'post_status'   => 'publish',
                'post_author'   => get_current_user_id(),
                'post_type'     => 'social_post'
            );
            $latest_post_id = wp_insert_post( $social_post_data );
            if($post['social_origin_url']) {
                add_post_meta($latest_post_id, 'social_origin_url', $post['social_origin_url']);
            }
            if($post['social_origin_id']) {
                add_post_meta($latest_post_id, 'social_origin_id', $post['social_origin_id']);
            }
            if($post['user']) {
                add_post_meta($latest_post_id, 'social_origin_user', $post['user']);
            }
            if($post['user']['team_account']) {
                add_post_meta($latest_post_id, $post['user']['site_name'].'_social_user_id', $post['user']['team_account']);
            }
            if($post['media']) {
                add_post_meta($latest_post_id, 'social_origin_thumbnail', $post['media']['url']);
            }
            $i++;
        }
        flush();
    }
    return $i;
}

This looks like a wordpress code? Have you tested print_r a single $post? In some frameworks, most of the objects are instance of a very complicated class. For ex. In laravel if you try to print_r a single model object it will result in a huge amount of data, so doing it in a loop can cause the timeout. Usually there is a way to get only the object's attributes. Hopefully this helps, maybe this should be in comment but my rep isn't enough for that