doucang9673 2015-11-27 08:53
浏览 38
已采纳

为什么我的数组增加了php laravel 5

I have an array which contains data from the database:

$responses = FormResponses::where('form_id', '>=', $phoneFirstElement->id)->where('form_id', '<=', $phoneLastElement->id)->get();
$responsesArray = $responses->toArray();

Then my nested loop:

  foreach ($phone as $key => $value2) // Populate Phone Numbers Horizontally
  {
      $sheet->cell($start.'9', $value2->phone);
      // This will fill the responses for each number
      foreach ($metrics as $key => $value)
      {
        $form_id = $value2->id;
        $metrics_id =  $value->id;
        $neededObjects = array_filter(
            $responsesArray,
            function ($e) use ($form_id, $metrics_id) {
                return $e['form_id'] == $form_id && $e['metrics_id'] == $metrics_id;
            }
        );
        var_dump($neededObjects); 
        exit;

        //$responses = FormResponses::where('form_id', '=', $value2->id)->where('metrics_id', '=', $value->id)->get();
        //$sheet->cell($start.$count, $neededObjects[$counter]['response']);
        $sheet->cell('C'.$count, $value->question);
        $sheet->cell('B'.$count, $value->description);
        $sheet->cell('A'.$count, $value->metrics_name);
        $counter++;
        $count++;
        $neededObjects = array();
      }
      $start++;
      $count = 10;
      //$counter = 0;
  }

My $responsesArray is the data which has lots of record, so I want to extract those data to get the specific one I need using array_filter and storing it in

$neededObjects

However when I make a var_dump I get something like

  array (size=1)
  0 => 
    array (size=7)
      'id' => int 141730
      'form_id' => int 4430
      'metrics_id' => int 1
      'response' => string 'Yes' (length=3)
      'remarks' => string '' (length=0)
      'created_at' => string '2015-11-23 19:30:07' (length=19)
      'updated_at' => string '2015-11-23 19:30:07' (length=19)

Then when the next records loops in

    array (size=1)
    1 => 
    array (size=7)
      'id' => int 141731
      'form_id' => int 4430
      'metrics_id' => int 2
      'response' => string 'Yes' (length=3)
      'remarks' => string '' (length=0)
      'created_at' => string '2015-11-23 19:30:07' (length=19)
      'updated_at' => string '2015-11-23 19:30:07' (length=19)

I don't understand why it is increamenting. The first one is 0 then next is 1 . Yes given that I am making an array_filter inside a nested loop but I didn't put any inreament vairables to it and the array filter will aways give me 1 record of data so I am expecting it atleast to be in index 0 or just a format like this:

array (size=7)
  'id' => int 141731
  'form_id' => int 4430
  'metrics_id' => int 2
  'response' => string 'Yes' (length=3)
  'remarks' => string '' (length=0)
  'created_at' => string '2015-11-23 19:30:07' (length=19)
  'updated_at' => string '2015-11-23 19:30:07' (length=19)
  • 写回答

2条回答 默认 最新

  • dongtuo4723 2015-11-27 09:04
    关注

    Any results that are returned by array_filter maintain their old keys. If you want the keys to reset then apply array_values to the result.

    If you only want one result then call current() or array_pop() on the result to get the first record.

    Even if array_filter returns one record, it will still be in the original array with the same key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持