进阶必备,php中对象转换数组与数组转换对象实

作者: 金沙澳门官网  发布:2019-09-26

次,一个前辈发了一道PHP笔试给我,题目大致如下

用stdClass转换数组为对象                                                                                  

 代码如下

Php代码

$arr = array('1','2','3');
 
foreach($arr as $key=>&$val){}
 
foreach($arr as $key=>$val){}
 
var_dump($arr);

 代码如下

问答案是什么?

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

看完我就懵了?根本不知道问题的考点在那里,看完两个foreach的区别在一个是取了地址,一个没有取地址,,这样会影响结果吗?

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

结果输出的结果居然是

用stdClass转换后:

 代码如下

Php代码

array(
  0 => string '1' (length=1)
  1 => string '2' (length=1)
  2 => &string '2' (length=1)
)

 代码如下

表示打印出来的数据为什么会这样?“3”去哪儿了?
造成这样的原因在于:&取地址符合。

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

 

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

用这种方式来说明foreach对数组的读取,比较直观。

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

然后循环就是指针的移动读取,而且由于是&取值,则直接读的是地址而是不简单的赋值。所以当第一个循环结束后。$val跟$arr[2]指的是同一个地址,所以$val值如果改变了,$arr['2']值也跟着改变了。

我应用的地方是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;
}

$arr = array('1','2','3');
 
foreach($arr as $key=>&$val){}
 
foreach($arr as $key=>$val){
 var_dump($arr);
}
var_dump($arr);

数组转换成对象

你就能看到变化的过程。和得到之前结果的原因

 代码如下

 

<?php
$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"                                      
}              

                                                  

本文由金沙澳门官网送注册58发布于金沙澳门官网,转载请注明出处:进阶必备,php中对象转换数组与数组转换对象实

关键词: