把andy强制转变为数组,这么些基类又有三个例外

作者:澳门娱乐

用stdClass转变数组为对象                                                                                  

php教程数组与对象相互转变方法

Php代码

function arrayToObject($e){
    if( gettype($e)!='array' ) return;
    foreach($e as $k=>$v){
        if( gettype($v)=='array' || getType($v)=='object' )
            $e[$k]=(object)arrayToObject($v);
    }
    return (object)$e;
}

function objectToArray($e){
    $e=(array)$e;
    foreach($e as $k=>$v){
        if( gettype($v)=='resource' ) return;
        if( gettype($v)=='object' || gettype($v)=='array' )
            $e[$k]=(array)objectToArray($v);
    }
    return $e;
}

 代码如下

关于数组转变为目的,在phpx有看见部分帖子切磋

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

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

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

把andy强制调换为数组,如同引用的福利些了,首借使产生了->

用stdClass转换后:

$andy = array();
$andy = (object)$andy;
$andy->a = 1;
$andy->b = 2;
$andy->c = 3;

Php代码

还嫌它麻烦,再用StdClass

 代码如下

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

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

stdClass 是PHP的五个基类,全部的类差不离都连任那个类,所以任哪一天候都足以被new,能够

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

让这些变量成为三个object。相同的时候,那几个基类又有三个出色的地点, 便是未有主意。在此间

stdClass是PHP的贰个基类,全部的类差不离都继承这一个类,所以任什么日期候都得以被new,可以让这么些变量成为二个object。同期,那些基类又有多个例外的地点,便是从未主意

再有一点是目的和数组资源消耗的难点:PHP5的靶子的独性格,对象在别的地点被调用,都

笔者动用的地点是simplexml中的simplexml_load_string()上,因为重回的全部都是指标,假如提取数额相比较困苦,所以选用了上边包车型客车函数

是援引地址型的,所以相对消耗的能源会少一 点。在其他页面为它赋值时是一贯改换,而不

 代码如下

是引用叁个拷贝。数组在程序的施用中应当是最平价的,不过数组的每一次被援引($a = $b)

function object_澳门娱乐6165,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;
}

,其实都以创办了三个别本,并且,数组被unset后,依旧占用了内部存储器(未有测验过)。还会有

数组调换来对象

叁个函数arrayobject,能够直接将数组转化为 对象那也是八个好法子啊。
ArrayObject::__construct -- Construct a new array object

 代码如下

<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');

$arrayobject = new ArrayObject($array);

print_r($arrayobject);
?>

ArrayObject Object
(
    [1] => one
    [2] => two
    [3] => three
)

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

试验了须臾间目的转换为数组,也能够强制调换

$arrayobject = new ArrayObject($array); 

$arr = (array)$obj;

var_dump($arrayobject); 
?>

如下例子调换结果,可对照:

运作结果:

Tag Object
(
    [name] => main
    [text] =>
    [parrent] =>
    [content] =>
    [props教程] => Array
        (
        )

    [inner] => Array
        (
        )

    [level] => 0
)

Array
(
    [name] => main
    [text] =>
    [parrent] =>
    [content] =>
    [props] => Array
        (
        )

    [inner] => Array
        (
        )

    [level] => 0
)

 代码如下

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"                                      
}              

                                                  

本文由澳门娱乐6165发布,转载请注明来源

关键词: