Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $.each()通过json循环而不是循环_Php_Jquery_Ajax_Json - Fatal编程技术网

Php $.each()通过json循环而不是循环

Php $.each()通过json循环而不是循环,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我有一个ajax调用,它返回json,我正试图将返回的项目发送到特定的输入文本id 这就是ajax: $.ajax({ url: "php/myfirstfile.php", type: "POST", data: $("#frameroof").serialize(), cache: false, dataType: "json", success: function (json) { $.each(json, function

我有一个ajax调用,它返回json,我正试图将返回的项目发送到特定的输入文本id

这就是ajax:

$.ajax({
    url: "php/myfirstfile.php",
    type: "POST",
    data: $("#frameroof").serialize(),
    cache: false,
    dataType: "json",
    success: function (json) {
        $.each(json, function () {
            $.each(json, function (key, value) {
                /// do stuff
                $('#' + key ).val(value);
            });
        });
    }
});
这就是返回的内容:[{a-frame:100}][{vertical:350}]

看起来我得到了两个阵列,而我需要一个来循环。我不确定

这里是php

if(isset($_POST["cart"])){
    $frameArray = ($_POST["cart"]);

    if(is_array($frameArray)){
        foreach($frameArray as $row){
            $catalogue = $row['catalogue'];
            $certification = $row['certification'];
            $catagory =  $row['catagory'];
            $subcatagory =  $row['subcatagory'];
            $length = $row['length'] ;

            $sql = "SELECT `price` AS  '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND  `certification` = '$certification' AND `catagory` = '$catagory' AND  `sub_catagory` = '$subcatagory' AND `length` = '$length' ";

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

            if (($result) && ($result->num_rows > 0)) {
                $results = array();
                //convert query result into an associative array
                while ($row = $result->fetch_assoc()) {
                    $results[] =  $row;
                }

                //dump all data from associative array converted from query result
                echo (json_encode($results,true));

                $result->free();
            }
        }
    }
}

$short_connect->close();

我相信你的问题很简单。在循环内部,您正在初始化结果数组并将其输出。这将导致创建一个新的结果数组,将其序列化为JSON,并为循环的每次迭代输出。因此,您发送给浏览器的不是JSON,而是几个JSON块一起运行

这是你需要做什么的基本想法:

<?php
// Initialize the output data
$results = array();

foreach($something as $a_something) {
    $results[] = do_something_to($a_something);
}

//Serialize and send the output data
echo (json_encode($results,true));

[{a-frame:100}][{vertical:350}]因为字符串不是有效的JSON,我想您正在尝试循环字符串。是否可以看到一些PHP的外观?这不是有价值的JSON。要么您试图手动输出JSON,而您永远不应该这样做,并且正在破坏它,要么您的服务器代码运行了两次。无论哪种方式,我们都需要查看服务器端代码。我真的比你知道的还要感激没问题!继续在这里交流,这样你会继续变得更好!
<?php

// Initialize the output data
$results = array();

if(isset($_POST["cart"])){
    $frameArray = ($_POST["cart"]);

    if(is_array($frameArray)){
        foreach($frameArray as $row){
            $catalogue = $row['catalogue'];
            $certification = $row['certification'];
            $catagory =  $row['catagory'];
            $subcatagory =  $row['subcatagory'];
            $length = $row['length'] ;

            $sql = "SELECT `price` AS  '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND  `certification` = '$certification' AND `catagory` = '$catagory' AND  `sub_catagory` = '$subcatagory' AND `length` = '$length' ";

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

            if (($result) && ($result->num_rows > 0)) {
                //convert query result into an associative array
                while ($row = $result->fetch_assoc()) {
                    //Add to the output data
                    $results[] =  $row;
                }

                $result->free();
            }
        }
    }
}

$short_connect->close();

//Serialize and send the output data
//dump all data from associative array converted from query result
echo (json_encode($results,true));