0
点赞
收藏
分享

微信扫一扫

tp5.1 php 对象转数组(items paginate)


一、实例:

代码

$list =  Db::name('order')
->field('id,number,price,type,state,time,pay_time,pay_number,time,express_state,express_id,mold')
->where($where)
->order(['id' => 'desc'])
->paginate($limit,false,['query' => request()->param(),'type' => 'page\Page','var_page' => 'page']);

输出是对象

dump($list);

tp5.1 php 对象转数组(items paginate)_php

要取得数组

tp5.1 php 对象转数组(items paginate)_数组方法_02

写法:

dump($list->items());exit;

 

 

二、其它对象输出方法:

object(WxPayUnifiedOrder)#49 (1) {
["values":protected] => array(6) {
["body"] => string(19) "菜鸟晋升课程|"
["out_trade_no"] => string(17) "20191117010631610"
["total_fee"] => float(100)
["notify_url"] => string(28) "http://www.cg.com/notify.php"
["trade_type"] => string(6) "NATIVE"
["product_id"] => int(1573923991)
}
}

转为数组方法一:

/**
* @desc 对象转化为数组
* @param $array object_data
* @return array
*/
function object_array($array)
{
if(is_object($array))
{
$array = (array)$array;
}
if(is_array($array))
{
foreach($array as $key=>$value)
{
$array[$key] = object_array($value);
}
}
return $array;
}

转为数组方法二:

$arrr=(array)$inputObj);
dump($arrr);

两种方式输出:

array(1) {
["*values"] => array(6) {
["body"] => string(19) "菜鸟晋升课程|"
["out_trade_no"] => string(17) "20191117011128528"
["total_fee"] => float(100)
["notify_url"] => string(28) "http://www.cg.com/notify.php"
["trade_type"] => string(6) "NATIVE"
["product_id"] => int(1573924288)
}
}

注意: ["*values"]

需要数组将使用数值键,从 0 开始并以 1 递增。

array_values() 函数

 

正确方法是:

    $arrr=array_values(object_array($inputObj));
$arrr=array_values((array)$inputObj);

输出:

array(1) {
[0] => array(6) {
["body"] => string(19) "菜鸟晋升课程|"
["out_trade_no"] => string(17) "20191117013844171"
["total_fee"] => float(100)
["notify_url"] => string(28) "http://www.cg.com/notify.php"
["trade_type"] => string(6) "NATIVE"
["product_id"] => int(1573925924)
}
}

取值:

          dump($arrr);
dump($arrr[0]);
dump($arrr[0]['out_trade_no']);die;

 

 

举报

相关推荐

0 条评论