php的物件轉陣列

2021-09-29 07:45:54 字數 1498 閱讀 5097

1.

//php stdclass object轉array  

function object_array($array)  

if(is_array($array))  

}  return $array;  }2.

$array = json_decode(json_encode(******xml_load_string($xmlstring)),true);

3.function object2array_pre(&$object) else

if (is_array($arr))

}return $arr;

}4.如果是10w的資料量的話,執行要進1s,結構再複雜些,可以達到3s,效能太差了,可以用以下替換:

function object2array(&$object)

json_decode(json_encode($array),true)
多層陣列和物件轉化的用途很簡單,便於處理webservice中多層陣列和物件的轉化

簡單的(array)和(object)只能處理單層的資料,對於多層的陣列和物件轉換則無能為力。

通過json_decode(json_encode($object)可以將物件一次性轉換為陣列,但是object中遇到非utf-8編碼的非ascii字元則會出現問題,比如gbk的中文,何況json_encode和decode的效能也值得疑慮。

下面上**:

<?php

function objecttoarray($d)

if (is_array($d))

else

}function arraytoobject($d)

else

}// useage:

// create new stdclass object     

$init = new stdclass;

// add some test data

$init->foo = "test data";

$init->bar = new stdclass;

$init->bar->baaz = "testing";

$init->bar->fooz = new stdclass;

$init->bar->fooz->baz = "testing again";

$init->foox = "just test";

// convert array to object and then object back to array

$array = objecttoarray($init);

$object = arraytoobject($array);

// print objects and array

print_r($init);

echo "\n";

print_r($array);

echo "\n";

print_r($object);

?>

php物件陣列轉陣列 php物件轉陣列的函式

關於php中想讓物件以陣列的形式訪問,這時候就需要使用到get object vars 函式了。先來介紹一下這個函式。官方文件是這樣解釋的 array get object vars object obj 返回由 obj 指定的物件中定義的屬性組成的關聯陣列。舉例 setlabel point 1 ...

PHP中把物件轉陣列的幾個方法

將物件轉換成陣列 param obj return mixed function object to array obj return arr 將二維陣列組裝成三維陣列 param arr param key return array function changetwotothree arr ke...

js物件轉陣列

1.array.from 方法,用於陣列的淺拷貝。就是將乙個類陣列物件或者可遍歷物件轉換成乙個真正的陣列。tips 1.object中必須有length屬性,返回的陣列長度取決於length長度 2.key 值必須是數值 2.object.values object tips 與第一種不同的是不需要...