首页 > php教程 > PHP源码 > 正文

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

WBOY
发布: 2016-06-08 17:24:58
原创
1001 人浏览过

本文章来介绍了在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"                                      
}              

                                                  

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板