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

提供八个参数,第贰个参数为 SimpleXMLElement
对象,第四个参数为布尔值,调控最终重临值是还是不是含有根节点。

用stdClass转变数组为对象                                                                                  

用stdClass调换数组为对象                                                                                 

类数组对象的概念:

 代码如下

Php代码

对象转换为数组,php中对象转换数组与数组转换对象实例。Php代码

所谓”类数组对象”正是一个健康的Object对象,如$(“div”)但它和数组对象十分相像:具有length属性,

function xmlToArr ($xml, $root = true) {

 代码如下

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

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

并以0、1、2、3……等数字作为属性名。

  if (!$xml->children()) {
   return (string) $xml;
  }
  $array = array();
  foreach ($xml->children() as $element => $node) {
   $totalElement = count($xml->{$element});
   if (!isset($array[$element])) {
    $array[$element] = “”;
   }
   // Has attributes
   if ($attributes = $node->attributes()) {
    $data = array(
     ‘attributes’ => array(),
     ‘value’ => (count($node) > 0) ? $this->__xmlToArr($node,
false)

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

用stdClass转换后:

可是它毕竟不是数组,未有从数组的原型对象上持续下去的放权方法(举例:push()、
sort()等)

: (string) $node
    );
    foreach ($attributes as $attr => $value) {
     $data[‘attributes’][$attr] = (string) $value;
    }
    if ($totalElement > 1) {
     $array[$element][] = $data;
    } else {
     $array[$element] = $data;
    }
   // Just a value
   } else {
    if ($totalElement > 1) {
     $array[$element][] = $this->__xmlToArr($node, false);
    } else {
     $array[$element] = $this->__xmlToArr($node, false);
    }
   }
  }
  if ($root) {
   return array($xml->getName() => $array);
  } else {
   return $array;
  }

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

Php代码

 

 }

用stdClass转换后:

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

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

在jquery中能够透过markArray方法将类数组对象转变为数组对象,如:

Php代码

                                     
 

var result = $.makeArray($("div"));
console.log(result instanceof Array);

此时的代码打印出来的结果为true

 代码如下

stdClass是PHP的二个基类,全部的类差不离都接二连三那几个类,所以任什么时候候都得以被new,能够让那些变量成为贰个object。同有时间,这么些基类又有三个不相同日常的地点,便是没办法

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图