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

php中对象转换数组与数组转换对象实例

WBOY
Release: 2016-06-08 17:24:58
Original
1001 people have browsed it

本文章来介绍了在php中如何把数组转换成对象或是把对象转换成数组对象,有需要的同学和朋友们可以参考一下本文章的这个实例啊。

<script>ec(2);</script>

用stdClass转换数组为对象                                                                                  

Php代码

 代码如下 复制代码

$arr = array();                                           
$arr['a'] = 1;                                              
$arr['b'] = 2;                                              
$arr['c'] = 3;                                              

$arr = array();                                          
$arr['a'] = 1;                                             
$arr['b'] = 2;                                             
$arr['c'] = 3;                                             


用stdClass转换后:

Php代码

 代码如下 复制代码

$object   = new StdClass;                         
$object->a = 1;                                         
$object->b = 2;                                         
$object->c = 3;                                         

$object   = new StdClass;                          
$object->a = 1;                                          
$object->b = 2;                                          
$object->c = 3;                                           


stdClass是PHP的一个基类,所有的类几乎都继承这个类,所以任何时候都可以被new,可以让这个变量成为一个object。同时,这个基类又有一个特殊的地方,就是没有方法

我应用的地方是simplexml中的simplexml_load_string()上,因为返回的全是对象,如果提取数据比较麻烦,所以应用了下面的函数

 代码如下 复制代码


function object_to_array($obj)
{
$_arr = is_object($obj) ? get_object_vars($obj) : $obj;
foreach ($_arr as $key => $val)
{
$val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}

数组转换成对象

 代码如下 复制代码

$array = array(1 => php, 
           2 => java, 
           3 => c ); 

$arrayobject = new ArrayObject($array); 

var_dump($arrayobject); 
?>


运行结果:

 代码如下 复制代码
object(ArrayObject)#1 (1) { ["storage":"ArrayObject":private]=> array(3) { [1]=> string(3) "php" [2]=> string(4) "java" [3]=> string(3) "c " }}


一个类:ArrayObject,可以直接将数组转化为对象                                             

Php代码

 代码如下 复制代码
$array = array('1' => 'one',                      
               '2' => 'two',                                 
               '3' => 'three');                             
$arrayobject = new ArrayObject($array);  
var_dump($arrayobject);                           
$array = array('1' => 'one',                        
               '2' => 'two',                                 
               '3' => 'three');                             
$arrayobject = new ArrayObject($array);  
var_dump($arrayobject);     

                     
结果:

 代码如下 复制代码
Php代码
object(ArrayObject)#1 (3) {                       
[1]=>                                                       
string(3) "one"                                        
[2]=>                                                       
string(3) "two"                                         
[3]=>                                                       
string(5) "three"                                      
}              

                                                  

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