Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.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 在jquery中使用json响应_Php_Jquery_Json_Javascript Objects - Fatal编程技术网

Php 在jquery中使用json响应

Php 在jquery中使用json响应,php,jquery,json,javascript-objects,Php,Jquery,Json,Javascript Objects,我想使用从控制器返回的json响应在jquery中创建一个对象 var cellContents = {'29-08-2018': '20','18-08-2018': '60'}; 这是我想要的格式,下面给出的是我的json响应 {"status":1,"result":[{"followup_datee":"17-08-2018","date":[{"fcount":"1"}]},{"followup_datee":"18-08-2018","date":[{"fcount":"1"}]}

我想使用从控制器返回的json响应在jquery中创建一个对象

var cellContents = {'29-08-2018': '20','18-08-2018': '60'};
这是我想要的格式,下面给出的是我的json响应

{"status":1,"result":[{"followup_datee":"17-08-2018","date":[{"fcount":"1"}]},{"followup_datee":"18-08-2018","date":[{"fcount":"1"}]}]}
我尝试了一些代码来制作我想要的格式,但是失败了。这就是我尝试的代码

var arr2 = [];
      //var cellContents = JSON.parse(data.result);

      for(var i=0; i<data.result.length; i++){
        var arr = [];
        for(var j=0; j<data.result[i].date.length; j++)
        { 
        arr.push(parseInt(data.result[i].date[j].fcount));
        } 
        var test = [data.result[i].followup_datee];
        arr2.push(test.concat(arr));
        } 
        var jsonString = JSON.stringify(arr2);
      console.log(jsonString);
这是我在php中使用的控制器

 public function getLeadcount()
{
    $status = 1;
    $arr = [];
    $sess = $this->session->userdata('user_id');
    $result = $this->mdl_lead_registration->getLeaddate($sess);
    if(!empty($result))
    {
        $status = 1;
        foreach($result as $res)
        {
            $res->{'date'} = '';
            $res->date = $this->mdl_lead_registration->getLeadcount($sess,$res->followup_datee);
        }

    }

   echo json_encode(array("status" => $status,"result" => $result));
}

我想你们都明白我的问题。任何类型的帮助都是值得欣赏的。

请使用以下代码:

var数据={“状态”:1,“结果”:[{“后续数据”:“17-08-2018”,“日期”:[{“fcount”:“1”}]},{“后续数据”:“18-08-2018”,“日期”:[{“fcount”:“1”}]}]};
var obj={};
对于(变量i=0;i控制台日志(obj)请给出一个例子-你从服务器上得到了什么,js结果是什么,你想要的结果是什么。我想要这样的结果{'17-08-2018':'1','18-08-2018':'1'}但我得到的结果是这样的[[[17-08-2018',1],“18-08-2018”,1]]为什么你的
date
属性是一个数组?你想要像
{'17-08-2018':[1],'18-08-2018':[1]}
这样的结果吗?我不想要结果中的任何数组格式我想要日期作为我的键,并计算为我的值,但你有
日期的数组!你想得到它们的总和吗?
 public function getLeadcount()
{
    $status = 1;
    $arr = [];
    $sess = $this->session->userdata('user_id');
    $result = $this->mdl_lead_registration->getLeaddate($sess);
    if(!empty($result))
    {
        $status = 1;
        foreach($result as $res)
        {
            $res->{'date'} = '';
            $res->date = $this->mdl_lead_registration->getLeadcount($sess,$res->followup_datee);
        }

    }

   echo json_encode(array("status" => $status,"result" => $result));
}