简体   繁体   English

通过Codeigniter中的json返回多个数组

[英]return multiple array through json in codeigniter

I want to return multiple array using json in codeigniter like this : 我想像这样在codeigniter中使用json返回多个数组:

   {
         "countData"
             [
                 {"flag":1,"count":3}
                 {"flag":0,"count":0}
                 {"flag":1,"count":2}
             ]
     }

I already tried : 我已经尝试过:

    $faqdata=array(
                       'count' => $faqdata['countdata']['resultdata']['count'],
                   )
     $listfaqcount['count_Data'][]=$faqdata;
     $listfaqcount['flag'] =1;
     $j_r=json_encode($listfaqcount);
     echo $j_r;

Like this way for two more array . 像这样又多了两个数组。 flag would be zero in else condition which I didn't mention here. 在其他条件下(我在此未提及), flag将为零。

How can I do this? 我怎样才能做到这一点? Please help. 请帮忙。

Thanks in advance 提前致谢

You can do something like this 你可以做这样的事情

$listfaqcount['countData'][0] = array(
    'flag' => 'flagValue',
    'count' => 'countValue'
);

While you looping you change the index with your key like that 在循环时,您可以像这样用键更改索引

$listfaqcount['countData'][1] = array(
    'flag' => 'flagValue',
    'count' => 'countValue'
);

and so on and while you encoding you can do like this 依此类推,在进行编码时,您可以像这样

echo json_encode($listfaqcount);

You need something like array_push 您需要像array_push这样的东西

$array['countdata'] = [];

create our sample array sets 创建我们的样本数组集

$somearray1 = ['flag'=>9,"count"=>5];
$somearray2 = ['flag'=>6,"count"=>6];
$somearray3 = ['flag'=>5,"count"=>7];
$somearray4 = ['flag'=>4,"count"=>8];

Basically what happens here is everytime you loop you push the array inside $array['countdata'] 基本上,这里发生的是每次循环时将数组推入$array['countdata']

array_push($array['countdata'], $somearray1); //loop 1 format and push
array_push($array['countdata'], $somearray2); //loop 2 format and push
array_push($array['countdata'], $somearray3); //loop 3 format and push
array_push($array['countdata'], $somearray4); //loop 4 format and push

Print the result beautifully or prettily 精美地打印结果

print_r(json_encode($array,JSON_PRETTY_PRINT));

result would be 结果将是

{
    "countdata": [
        {
            "flag": 9,
            "count": 5
        },
        {
            "flag": 6,
            "count": 6
        },
        {
            "flag": 5,
            "count": 7
        },
        {
            "flag": 4,
            "count": 8
        }
    ]
}

this is just an example you could push your own formatted array. 这只是一个示例,您可以推送自己的格式化数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM