首頁  >  文章  >  php教程  >  php中对象转换数组与数组转换对象实例

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

WBOY
WBOY原創
2016-06-08 17:24:58992瀏覽

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

                                                  

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn