Home > php教程 > PHP源码 > body text

Example of conversion between PHP object and array

WBOY
Release: 2016-07-06 13:34:21
Original
1806 people have browsed it

The conversion between PHP objects (objects) and arrays (array) is used a lot in our development. For this problem, we have compiled some examples today, as shown below.

Arrays are the soul of PHP and are very powerful, but sometimes object-oriented programming is also very convenient. Switching between arrays and objects is also common:

Example 1
 代码如下 复制代码

/**
 * 数组 转 对象
 *
 * @param array $arr 数组
 * @return object
 */
function array_to_object($arr)
{
 if (gettype($arr) != 'array')
 {
  return;
 }
 foreach ($arr as $k => $v)
 {
  if (gettype($v) == 'array' || getType($v) == 'object')
  {
   $arr[$k] = (object)array_to_object($v);
  }
 }

 return (object)$arr;
}

/**
 * 对象 转 数组
 *
 * @param object $obj 对象
 * @return array
 */
function object_to_array($obj)
{
 $obj = (array)$obj;
 foreach ($obj as $k => $v)
 {
  if (gettype($v) == 'resource')
  {
   return;
  }
  if (gettype($v) == 'object' || gettype($v) == 'array')
  {
   $obj[$k] = (array)object_to_array($v);
  }
 }

 return $obj;
}

The code is as follows Copy code

/**
* Array to object
*
* @param array $arr array
* @return object
​*/
function array_to_object($arr)
{
if (gettype($arr) != 'array')
{
return;
}
foreach ($arr as $k => $v)
{
if (gettype($v) == 'array' || getType($v) == 'object')
{
$arr[$k] = (object)array_to_object($v);
}
}

 代码如下 复制代码

class Test{
public $a;
public $b;
public function __construct($a) {
$this->a = $a;
    }
}
 
//对象转数组,使用get_object_vars返回对象属性组成的数组
function objectToArray($obj){
    $arr = is_object($obj) ? get_object_vars($obj) : $obj;
    if(is_array($arr)){
        return array_map(__FUNCTION__, $arr);
    }else{
        return $arr;
    }
}
 
//数组转对象
function arrayToObject($arr){
    if(is_array($arr)){
        return (object) array_map(__FUNCTION__, $arr);
    }else{
        return $arr;
    }
}
 
$test = new Test('test1');
$test->b = new Test('test2');
 
print_r($test);
$array = objectToArray($test);
print_r($array);
$object = arrayToObject($array);
print_r($object);

return (object)$arr;
}<script>ec(2);</script> /**
* Object to array
*
* @param object $obj object
* @return array
​*/
function object_to_array($obj)
{
$obj = (array)$obj;
foreach ($obj as $k => $v)
{
if (gettype($v) == 'resource')
{
Return;
}
if (gettype($v) == 'object' || gettype($v) == 'array')
{
$obj[$k] = (array)object_to_array($v);
}
} return $obj;
}
Example 2
The code is as follows Copy code
<🎜>class Test{
Public $a;
Public $b;
Public function __construct($a) {
           $this->a = $a;
}
}

//Convert object to array, use get_object_vars to return an array composed of object attributes
function objectToArray($obj){
$arr = is_object($obj) ? get_object_vars($obj) : $obj;
If(is_array($arr)){
          return array_map(__FUNCTION__, $arr);
}else{
         return $arr;
}
}

//Convert array to object
function arrayToObject($arr){
If(is_array($arr)){
          return (object) array_map(__FUNCTION__, $arr);
}else{
         return $arr;
}
}

$test = new Test('test1');
$test->b = new Test('test2');

print_r($test);
$array = objectToArray($test);
print_r($array);
$object = arrayToObject($array);
print_r($object);
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template