Ajax将数据推送到数组

时间:2018-08-29 19:11:59

标签: php jquery arrays json ajax

现在大约几天后,我正试图从php页面(数组)中获取数据并将该数组的数据存储在一个jquery数组中。问题是当我像这样将数据发送到php页面时:

$.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);
    }

});

我正在接收代码发送给我的数组相同的数据,因为我希望php页面向我发送其他包含其他ID(不同于我在jquery中发送的数组的ID)的文章:

<?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();
?>

感谢您的阅读和帮助,我希望这很清楚。

1 个答案:

答案 0 :(得分:0)

您可能需要在添加新项目之前清空数组:

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);
},