美文网首页
php 数组对象转普通数组

php 数组对象转普通数组

作者: 风度翩翩的程序猿 | 来源:发表于2020-06-02 14:54 被阅读0次

今天给大家介绍一下,php中的数组对象转普通数组的知识,这些知识在开发中遇到的并不多,但是也很使用,封装一个函数,调用就行啦,好了,咱们往下看:

就比如这样的数据类型

object(stdClass)#13 (29) {
  ["id"] => string(36) "a1dcbc4e-4f08-4dcb-b228-a5e46716a0a1"
  ["state"] => string(9) "调度中"
  ["source"] => string(12) "手工录入"
  ["no_inner"] => string(16) "2020043000000005"
  ["no_outer"] => string(0) ""
  ["no_customer"] => string(0) ""
  ["create_time"] => string(19) "2020-04-30 06:45:00"
  ["customer_name"] => string(12) "测试客户"
  ["trans_mode"] => string(6) "零担"
  ["trans_carrier_name"] => string(15) "测试承运商"
  ["com_name"] => string(18) "绿百运供应链"
  ["src_shipper"] => string(27) "苏州-望亭仓(B2C)11"
  ["src_linkman"] => string(12) "苏州绿叶"
  ["src_phone"] => string(32) "4009005528,4009005528,4009005528"
  ["src_cities"] => array(3) {
    [0] => string(9) "江苏省"
    [1] => string(9) "苏州市"
    [2] => string(9) "虎丘区"
  }
  ["src_address"] => string(81) "江苏省苏州市相城区江苏省苏州市浒墅关工业园区安杨路198号"
  ["dest_receiver"] => string(12) "叶春梅157"
  ["dest_linkman"] => string(20) "叶春梅15707906897"
  ["dest_phone"] => string(11) "15707906797"
  ["dest_cities"] => array(3) {
    [0] => string(9) "江西省"
    [1] => string(9) "新余市"
    [2] => string(9) "渝水区"
  }
  ["dest_address"] => string(87) "江西省新余市渝水区江西省新余市茶山大道文新路幸福布艺窗帘店"
  }
}

用起来不是很方便,并且不是很熟悉这种数据类型,那咱们就要转化一下

array(29) {
  ["id"] => string(36) "a1dcbc4e-4f08-4dcb-b228-a5e46716a0a1"
  ["state"] => string(9) "调度中"
  ["source"] => string(12) "手工录入"
  ["no_inner"] => string(16) "2020043000000005"
  ["no_outer"] => string(0) ""
  ["no_customer"] => string(0) ""
  ["create_time"] => string(19) "2020-04-30 06:45:00"
  ["customer_name"] => string(12) "测试客户"
  ["trans_mode"] => string(6) "零担"
  ["trans_carrier_name"] => string(15) "测试承运商"
  ["com_name"] => string(18) "绿百运供应链"
  ["src_shipper"] => string(27) "苏州-望亭仓(B2C)11"
  ["src_linkman"] => string(12) "苏州绿叶"
  ["src_phone"] => string(32) "4009005528,4009005528,4009005528"
  ["src_cities"] => array(3) {
    [0] => string(9) "江苏省"
    [1] => string(9) "苏州市"
    [2] => string(9) "虎丘区"
  }
  ["src_address"] => string(81) "江苏省苏州市相城区江苏省苏州市浒墅关工业园区安杨路198号"
  ["dest_receiver"] => string(12) "叶春梅157"
  ["dest_linkman"] => string(20) "叶春梅15707906897"
  ["dest_phone"] => string(11) "15707906797"
  ["dest_cities"] => array(3) {
    [0] => string(9) "江西省"
    [1] => string(9) "新余市"
    [2] => string(9) "渝水区"
  }
  ["dest_address"] => string(87) "江西省新余市渝水区江西省新余市茶山大道文新路幸福布艺窗帘店"
  }
}

这样的数据咱们就很熟悉,就是普通的数组,用起来也很方便,该怎么转化呢?
封装一个函数就行啦:

 // 对象数值转普通数组
    function std_class_object_to_array($stdclassobject)
    {
        $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) : $stdclassobject;
        foreach ($_array as $key => $value) {
            $value = (is_array($value) || is_object($value)) ? $this->std_class_object_to_array($value) : $value;
            $array[$key] = $value;
        }
       return $array;
    }

这样就OK啦,相当简单!!!!!

相关文章

网友评论

      本文标题:php 数组对象转普通数组

      本文链接:https://www.haomeiwen.com/subject/ruuuzhtx.html