语法:get_object_var($object),返回一个数组。获取$object对象中的属性,组成一个数组
<?php class person{ public $name="王美人"; public $age = 25; public $birth; } $p = new person(); print_r(get_object_vars($p)); ?>
输出结果:
Array ( [name] => 王美人 [age] => 25 [birth] => )
在laravel数据库查询中使用toArray()方法外层已经转化成了数组,但是内层依然是对象的问题。
array (size=1) 0 => object(stdClass)[1804] public 'id' => int 17 public 'ip' => string '192.168.0.60' (length=12) public 'access_time' => int 1615625654 public 'access_count' => int 1 public 'last_time' => int 1615625672
在vendorlaravelframeworksrcIlluminateSupportCollection.php中增加方法 getArrayList()
public function toArray() { return array_map(function ($value) { return $value instanceof Arrayable ? $value->toArray() : $value; }, $this->items); } /*增加方法*/ public function getArrayList(){ return array_map('get_object_vars', $this->items); }
查询时使用:
DB::table('api_log')->select('id','ip','access_time','access_count','last_time')->where('ip',$logInfo['ip'])->orderBy('id','desc')->limit(1)->get()->getArrayList()[0];
显示结果:
array (size=5) 'id' => int 17 'ip' => string '192.168.0.60' (length=12) 'access_time' => int 1615625654 'access_count' => int 0 'last_time' => int 1615625654
Array ( [name] => 王美人 [age] => 25 [birth] => )
最新评论