对象数组搜索PHP Laravel 5

时间:2015-11-26 11:11:24

标签: php arrays laravel multidimensional-array

我正在使用Laravel 5,我想进行数组搜索。但问题是查询返回一个对象,所以我所做的就是对它进行类型转换,当我将它强制转换为数组时,仍有一个对象。

所以我有这张桌子

FormResponses
->id
->form_id
->metrics_id

然后我的查询

$responses = FormResponses::where('form_id', '>=', 1)->where('form_id', '<=', 500)->get();
$responses =  (array) $responses;

$neededObjects = array_filter(
          $responses,
          function ( $e) {

              return $e->form_id == 1 && $e->metrics_id == 1;
          }
      );

如您所见,$needObjects是我过滤后数组的变量。我这样做是为了搜索记录。你可以看到

return $e->form_id == 1 && $e->metrics_id == 1;

这意味着我想搜索数组中

所有的记录

form_id = 1metrics_id = 1

这是$responses在未转换为数组

时的输出
    object(Illuminate\Database\Eloquent\Collection)[3423]
      protected 'items' => 
        array (size=3072)
          0 => 
            object(App\Http\Models\FormResponses)[3424]
              protected 'table' => string 'forms_responses' (length=15)
              protected 'connection' => null
              protected 'primaryKey' => string 'id' (length=2)
              protected 'perPage' => int 15
              public 'incrementing' => boolean true
              public 'timestamps' => boolean true
              protected 'attributes' => 
                array (size=7)
                  ...
              protected 'original' => 
                array (size=7)
                  ...
              protected 'relations' => 
                array (size=0)
                  ...
              protected 'hidden' => 
                array (size=0)
                  ...
              protected 'visible' => 
                array (size=0)
                  ...
              protected 'appends' => 
                array (size=0)
                  ...
              protected 'fillable' => 
                array (size=0)
                  ...
              protected 'guarded' => 
                array (size=1)
                  ...
              protected 'dates' => 
                array (size=0)
                  ...
              protected 'dateFormat' => null
              protected 'casts' => 
                array (size=0)
                  ...
              protected 'touches' => 
                array (size=0)
                  ...
              protected 'observables' => 
                array (size=0)
                  ...
              protected 'with' => 
                array (size=0)
                  ...
              protected 'morphClass' => null
              public 'exists' => boolean true
              public 'wasRecentlyCreated' => boolean false
$responses = (array) $responses;

之后

        array (size=1)
      '�*�items' => 
        array (size=3072)
          0 => 
            object(App\Http\Models\FormResponses)[3424]
              protected 'table' => string 'forms_responses' (length=15)
              protected 'connection' => null
              protected 'primaryKey' => string 'id' (length=2)
              protected 'perPage' => int 15
              public 'incrementing' => boolean true
              public 'timestamps' => boolean true
              protected 'attributes' => 
                array (size=7)
                  ...
              protected 'original' => 
                array (size=7)
                  ...
              protected 'relations' => 
                array (size=0)
                  ...
              protected 'hidden' => 
                array (size=0)
                  ...
              protected 'visible' => 
                array (size=0)
                  ...
              protected 'appends' => 
                array (size=0)
                  ...
              protected 'fillable' => 
                array (size=0)
                  ...
              protected 'guarded' => 
                array (size=1)
                  ...
              protected 'dates' => 
                array (size=0)
                  ...
              protected 'dateFormat' => null
              protected 'casts' => 
                array (size=0)
                  ...
              protected 'touches' => 
                array (size=0)
                  ...
              protected 'observables' => 
                array (size=0)
                  ...
              protected 'with' => 
                array (size=0)
                  ...
              protected 'morphClass' => null
              public 'exists' => boolean true
              public 'wasRecentlyCreated' => boolean false

2 个答案:

答案 0 :(得分:3)

Laravel Collections有toArray()方法:

$responsesArray = $responses->toArray()

Documentation

答案 1 :(得分:0)

为什么要尝试发布处理数据?只需查询您想要的记录:

$responses = FormResponses::where('form_id', '=', 1)->where('metrics_id', '=', 1)->get();