如何将JSON限制为MYSQL导出到前1万行?

时间:2020-04-23 01:43:49

标签: php mysql json

我正在使用JSON到MYSQL转换PHP代码,该代码从JSON提要中读取数据。下面是我的代码。我遇到的问题是JSON提要大约300万行。我只想选择前10K行,然后将其导出到MYSQL。如何在下面的代码中实现该目标?

<?php
@ini_set( 'max_execution_time', 0);

require_once 'db_connect.php';

/* DEFINE TABLE NAME AND SOURCE URL HERE */
$str_tbl_name   = 'table_name';
$str_source_url = 'https://www.json-feed.com/example';

/* FETCH DATA FROM RESOURCE LINK */
$str_source_content = file_get_contents($str_source_url);
$arr_data           = json_decode($str_source_content);

$str_date_time  = date('Y-m-d H:i:s');
$str_start_date = date('Y-m-d H:i:s', strtotime($arr_data->meta->start_date));
$str_end_date   = date('Y-m-d H:i:s', strtotime($arr_data->meta->end_date));
$arr_value_sql  = [];
foreach ($arr_data->data as $data) {
    /* DATA CLEANSING */
    $data->source_subregion        = !empty($data->source_subregion) ? $con_db->real_escape_string($data->source_subregion) : null;
    $data->source_country_code     = !empty($data->source_country_code) ? $con_db->real_escape_string($data->source_country_code) : null;
    $str_input_name                = !empty($data->metadata->search_name) ? $con_db->real_escape_string($data->metadata->search_name) : null;
    $data->document_url            = !empty($data->document_url) ? $con_db->real_escape_string($data->document_url) : null;
    $str_document_tags             = !empty((array) $data->document_tags) ? $con_db->real_escape_string(serialize($data->document_tags)) : null;
    $data->document_sentiment      = !empty($data->document_sentiment) ? $con_db->real_escape_string($data->document_sentiment) : null;
    $data->source_name             = !empty($data->source_name) ? $con_db->real_escape_string($data->source_name) : null;
    $data->document_publish_date   = !empty($data->document_publish_date) ? $con_db->real_escape_string($data->document_publish_date) : null;
    $str_document_matched_keywords = !empty((array) $data->document_matched_keywords) ? $con_db->real_escape_string(implode(',', $data->document_matched_keywords)) : null;
    $data->document_language_code  = !empty($data->document_language_code) ? $con_db->real_escape_string($data->document_language_code) : null;
    $str_document_key_phrases      = !empty((array) $data->document_key_phrases) ? $con_db->real_escape_string(implode(',', $data->document_key_phrases)) : null;
    $data->document_hit_sentence   = !empty($data->document_hit_sentence) ? $con_db->real_escape_string($data->document_hit_sentence) : null;
    $data->document_hidden         = ($data->document_hidden === true) ? 'TRUE' : 'FALSE';
    $str_document_authors          = !empty($data->document_authors[0]->name) ? $con_db->real_escape_string($data->document_authors[0]->name) : null;
    $data->document_city           = !empty($data->document_city) ? $con_db->real_escape_string($data->document_city) : null;

    $str_value_sql = " ("
            . "'{$data->document_publish_date}'"
            . ", '{$data->document_url}'"
            . ", '{$data->source_name}'"
            . ", '{$str_document_authors}'"
            . ", '{$data->source_country_code}'"
            . ", '{$data->source_subregion}'"
            . ", '{$data->document_language_code}'"
            . ", '{$data->source_reach}'"
            . ", '{$data->source_ave}'"
            . ", '{$data->document_sentiment}'"
            . ", '{$str_document_key_phrases}'"
            . ", '{$str_input_name}'"
            . ", '{$str_document_matched_keywords}'"
            . ", '{$data->document_city}'"
            . ")";
    array_push($arr_value_sql, $str_value_sql);
}

2 个答案:

答案 0 :(得分:1)

只需使用计数器或数组索引,并在获得所需的行数后从foreach中断。

计数器:

$counter=0;
$values=[];
foreach($arr_data->data as $data){
    array_push($values, ...);
    if($counter++>=10000)
        break;
}
//Insert rows

阵列键(假设顺序阵列):

foreach($arr_data->data as $key=>$data){
    if($key>=10000)
        break;
    array_push($values, ...);
}
//Insert rows

答案 1 :(得分:1)

您可以使用for loop代替foreach

如果问题是内存不足,无法在API上对结果进行分页,但可以对整个内容进行解码,则可以尝试运行第一遍以删除不需要的元素和可用内存,然后正常处理。

// Delete unwanted elements
for($i=10000; $i<count($arr_data->data); $i++) { 
    unset($arr_data->data[$i]);
}

foreach ($arr_data->data as $data) {
    // Your old loop
}