dsgoj7457 2018-08-29 19:11
浏览 93

Ajax将数据推送到数组

It's now about few days that i'm trying to get the data from an php page (array) and to store the array's data in a jquery array. The problem is that when i'm sending the data to the php page like this:

$.ajax({
    type: "POST",
    url: 'test2.php?pro=<?php echo $project_name ?>',
    data: {verifie : JSON.stringify(PassArray)},
    cache:false,
    success:function(html) {
         console.log(html);
         $.each( JSON.parse(html), function( key, value ) {
             var value2 = value
             PassArray.push(value2);
         });
         console.log(PassArray);
    },
    error:function(err){
    console.log(err);
    }

});

I'm receiving the data the code send me the same array, because i want the php page to send me other articles that contains other id's (different from the id's of the array that i sent in jquery):

<?php
$verifie2 = array();
$final = array();
function keyword_sys() {
mb_internal_encoding('UTF-8');

global $verifie2,$final;

$string = $_GET["pro"];

$verifie = json_decode($_POST["verifie"]);

$project = array();
$stopwords = array();

$count = 0;

$string = preg_replace('/[\pP]/u', '', trim(preg_replace('/\s\s+/iu', '', mb_strtolower($string))));
$matchWords = array_filter(explode(' ',$string) , function ($item) use ($stopwords) { return !($item == '' || in_array($item, $stopwords) || mb_strlen($item) <= 2 || is_numeric($item));});

foreach ($matchWords as &$value) {
    include("connect.php");

    if($verifie == null || $verifie = ""){
        $sql = "SELECT * FROM keyword WHERE keyword = '$value' LIMIT 5";
    }else{
        $sql = "SELECT * FROM keyword WHERE keyword = '$value' AND project_id NOT IN ( '" . implode( "', '" , (array)$verifie ) . "' ) LIMIT 5";
    }

    $result = $conn->query($sql);

    foreach  ($conn->query($sql) as $row) {

        $project_id = $row["project_id"];
        $keyword = $row["keyword"];

        if (!in_array($project_id, $verifie2)) {

            array_push($verifie2, $project_id);

            $compaire = array();

            $sql2 = "SELECT * FROM keyword WHERE project_id = '$project_id'";
            $result2 = $conn->query($sql2);
            foreach  ($conn->query($sql2) as $row2) {

                $key = $row2["keyword"];
                array_push($compaire, $key);

            }

            $nb_array = count($compaire);
            $nb_array2 = count($project);

            $project2[] = $project_id;
            $project1[] = count(array_diff($compaire,$matchWords));

            reset($compaire); 

        }

    }

    foreach  ($conn->query($sql) as $row) {
        $project_id = $row["project_id"];
        if (!in_array($project_id, $project)) {
            array_push($project, $project_id);
            $count = $count + 1;
        }
    }
}

$n = count($project2);

for ($x = 0; $x < $n; $x++) {
    $val = array_keys($project1, min($project1))[0];
    array_push($final, $project2[$val]);
    unset($project1[$val]);
    unset($project2[$val]);
} 

print json_encode($final, JSON_PRETTY_PRINT);
}
keyword_sys();
?>

Thanks for reading and for your help, I hope it's clear.

  • 写回答

1条回答 默认 最新

  • dongyunqin7307 2018-08-29 19:30
    关注

    you may need to empty the array before adding new items:

    success: function(html) {
             console.log(html);
             PassArray = []; // added line
             $.each( JSON.parse(html), function( key, value ) {
                 var value2 = value
                 PassArray.push(value2);
             });
             console.log(PassArray);
    },
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大