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
Php 如何“计数”查询结果并将其作为JSON格式的数组发送? 细节_Php_Arrays_Json_Laravel_Laravel 4 - Fatal编程技术网

Php 如何“计数”查询结果并将其作为JSON格式的数组发送? 细节

Php 如何“计数”查询结果并将其作为JSON格式的数组发送? 细节,php,arrays,json,laravel,laravel-4,Php,Arrays,Json,Laravel,Laravel 4,我想 数一数我询问的所有分销商 在JSON文件中发送它 我知道我总共有89个分销商,我做了这个dd(count($distributors)) 我确信这方面的最佳做法是什么 这是我试过的 我初始化$count=0 每次循环执行时递增1$count=$count+1 将结果发送到阵列'count'=>$count->toArray()作为我的分发服务器阵列的一部分 这是我的密码 结果 代码将不会运行,因为我的$distributor数组不存在 如果我关闭'count'=>$count->t

我想

  • 数一数我询问的所有分销商
  • 在JSON文件中发送它
  • 我知道我总共有89个分销商,我做了这个
    dd(count($distributors))

  • 我确信这方面的最佳做法是什么

这是我试过的
  • 我初始化
    $count=0
  • 每次循环执行时递增1
    $count=$count+1
  • 将结果发送到阵列
    'count'=>$count->toArray()
    作为我的分发服务器阵列的一部分
这是我的密码 结果 代码将不会运行,因为我的$distributor数组不存在

如果我关闭
'count'=>$count->toArray()
,它将运行

更新
  • 我使用的是Laravel4.0
  • 该代码是我的UrlController.php的一部分

    • 对不起,我可能错了

      我不是拉威尔的专家

      但是这个片段是关于什么的呢

      此索引函数是模型的一部分

      @evoque2015正在尝试将一些自定义数组放入$distributors[$distributor->id]

      如果这是你的目标,你能用任何简单的“count”值做任何测试吗

      我的猜测是:“count”索引对于您的分发服务器::where函数是不可接受的

      (如果可以接受-显示为“count”的值,即使返回错误的结果/数据也不会破坏代码)

      因此,我会尝试将此参数的名称更改为“my_custom_count”, 是否应该在分发服务器模型声明中的某个位置声明

      我们发现:

      这似乎证明了我的猜测

      因此,我们需要更改模型类,如:

      class Distributor extends Eloquent {
      
          protected $table = 'distributors';//or any you have already
      
          public function toArray()
          {
              $array = parent::toArray();
              $array['count'] = $this->count;
              return $array;
          }
      
      .....
      }
      

      而且可能会对模型进行更多更改,或者只是在表中添加“count”列:-)

      将这种计数添加到结果中真的没有多大意义。只发送结果并让客户机进行计数要简单得多。因为关于你实际拥有多少分销商的信息就在你的分销商列表中。只是它的长度

      下面是一个javascript示例(和
      $.ajax
      ,尽管这并不重要)

      型号:

      控制器:

      $distributors = Distributor::whereNotIn('type', ['OEM', 'MKP'])
          ->with('country', 'user')->get();
      
      $count = 0;
      $distributors->each(function(Distributor $distributor) use(&$count) {
          $distributor->count = $count;
          $count++;
      });
      
      return Response::json($distributors);
      

      $count=0
      $count=$count+1,然后尝试使用
      $count->toArray()
      ?你把一个整数当作一个对象来对待。@sjagr:为了解决这个问题,你有什么建议?你能帮我一下吗?你能不能不只是做一下计数?不知道为什么你试图让
      count
      成为一个数组,而它只是一个数字。或者(如果你真的想要它作为一个数组)
      'count=>array('count'=>$count)
      ,但这对我来说似乎是多余的。我不知道。这不是我写的代码。这取决于你想要什么
      count
      对你意味着什么。如果使用的是聚合计数,请单独使用
      'count'=>$count
      。如果您试图获取查询的计数,请像您自己所说的那样使用
      'count'=>count($distributors)
      。我不知道,但我很难想象您为什么要将结果中所有项目的计数与结果一起发送。当你收到回复时,你不能数一数吗?e、 在javascript中:
      data.length
      感谢您的尝试;我错了吗?我很确定问题就在那里-模型属性声明…:-)回答得好!您是否介意更新您的答案,并演示如何使用php获得相同的结果?我正试着看那个数字如果你不介意的话。:)什么意思?php的
      .length
      count()
      。或者我没有正确理解你的问题。。。
      $.ajax({
          url: 'get/distributors',
          method: 'GET'
      }).done(function(data){
          var count = data.length;
      });
      
      class Distributor extends Eloquent {
          public function country()
          {
              return $this->hasOne('Country');
          }
      
          public function addresses()
          {
              return $this->hasMany('Address');
          }
      
          public function hqAddress()
          {
              return $this->addresses()->where('type', 'Hq')->first();
          }
      
          public function user()
          {
              return $this->hasOne('User');
          }
      }
      
      $distributors = Distributor::whereNotIn('type', ['OEM', 'MKP'])
          ->with('country', 'user')->get();
      
      $count = 0;
      $distributors->each(function(Distributor $distributor) use(&$count) {
          $distributor->count = $count;
          $count++;
      });
      
      return Response::json($distributors);