将PHP数组转储为条件内容的JSON [英] Dump PHP Array as JSON of conditional content

查看:119
本文介绍了将PHP数组转储为条件内容的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于内容的类型的条件内容",例如:

I have "conditional content" base on the type of content, like so:

//--- Here goes a nice SQL where I get the content, this works ok. ---
if(mysqli_num_rows($niceSQL) > 0) {
    while($something = mysqli_fetch_array($niceSQL)) {

        $type = $something["type"];

        if ("type" == "A") {
            $data1 = $something["data1"];
            $data2 = $something["data2"];
            $data3 = $something["data3"];

            $something_A = array(
                "one" => $data1,
                "two" => $data2,
                "three" => $data3,
            );

            echo json_encode($something_A, JSON_FORCE_OBJECT);
        } else if ("type" == "B") {
            $data1 = $something["data1"];
            $data2 = $something["data2"];
            $data3 = $something["data3"];

            $something_B = array(
                "one" => $data1,
                "two" => $data2,
                "three" => $data3,
            );

            echo json_encode($something_B, JSON_FORCE_OBJECT);
        }
    }
}

我正在尝试将每个数组都以JSON格式转储,以使用jQuery Each处理它.

I'm trying to dump every array as JSON for handle it with jQuery Each.

问题是所有同时被打印"(回音)的东西,因此AJAX响应是" parsererror ".我通过这种方式收到此错误:

The problem is all that gets "printed" (echoed) at same time, so the AJAX response is "parsererror". I get this error in this way:

(ajax and config here).fail(function( jqXHR, textStatus ) {
    console.log( "Request failed: " + textStatus );
})

那么,如何单独但一起打印每个$something_A$something_B?在一起意味着仅一个ajax请求,因此我可以使用jQuery Each处理内容.

So, how can I print every $something_A or $something_B independently but together? Together means only one ajax request, so I can handle the content with jQuery Each.

如果仅打印第一个$something_A或第一个$something_B,则可以执行每个"循环(这就是为什么我认为PHP"json_encode"功能可以正常工作的原因),但这对我没有用(因为我只收到许多东西"中的第一个.

If I print only the first $something_A or the first $something_B then I can do the "each" loop (that's why I assume the PHP "json_encode" function is working properly), but it's not useful to me (because I'm receiving only the 1st of many "somethings").

注意:我可以更改转储的逻辑,而不是更改内容的获取方式(SQL + while).

Note: I can change the logic of the dumping, not how I get the content (SQL + while).

推荐答案

每个ajax请求只能发送一个json输出

you can only send one json output per ajax request

创建一个具有2个属性'A''B'的外部主控数组,每个属性均为空数组,然后将每一行推入相应的行,并在循环后回显主控数组

create an outer master array that has 2 properties 'A' and 'B' that are each empty arrays then push each row to appropriate one, and after loop echo the master array

$output= array(
   'A'=>array(),
   'B'=>array()
);

while($something = mysqli_fetch_array($niceSQL)) {

    $type = $something["type"];

    if ("type" == "A") {
       .......
       $output['A'][] = $something_A;

    } else if ("type" == "B") {
       ......
       $output['B'][] = $something_B;
    }
}

echo json_encode($output);

然后在jQuery中,您将拥有一个对象,该对象具有2个属性,每个属性都是一个数组

Then in jQuery you have a single object with 2 properties that are each an array

这篇关于将PHP数组转储为条件内容的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆