一個小小的示例方法...
/**
* 陣列轉換物件
*
* @param $e 陣列
* @return object|void
*/
public function arrayToObject($e)
{
if (gettype($e) != 'array') return;
foreach ($e as $k => $v) {
if (gettype($v) == 'array' || getType($v) == 'object')
$e[$k] = (object)$this->arrayToObject($v);
}
return (object)$e;
}
/**
* 物件轉換陣列
*
* @param $e StdClass物件例項
* @return array|void
*/
public 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)$this->objectToArray($v);
}
return $e;
}
結果
-
陣列轉物件
-
物件轉陣列
本作品採用《CC 協議》,轉載必須註明作者和本文連結