本文整理匯總了PHP中ArrayHelper::toArray方法的典型用法代碼示例。如果您正苦於以下問題:PHP ArrayHelper::toArray方法的具體用法?PHP ArrayHelper::toArray怎麽用?PHP ArrayHelper::toArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ArrayHelper
的用法示例。
在下文中一共展示了ArrayHelper::toArray方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __construct
public function __construct(array $data = NULL, $keyColumnNames = NULL, $columnPrefix = NULL, $columnSuffix = NULL)
{
parent::__construct();
$this->keyColumnNames = ArrayHelper::toArray($keyColumnNames);
$this->columnPrefix = $columnPrefix;
$this->columnSuffix = $columnSuffix;
$this->data = $data;
}
示例2: toArray
public static function toArray($object, $recursive = true)
{
if (is_array($object)) {
$res = [];
foreach ($object as $key => $each) {
if (is_array($each) || is_object($each)) {
$res[$key] = ArrayHelper::toArray($each, $recursive);
} else {
$res[$key] = $each;
}
}
return $res;
}
if (!is_object($object)) {
return [$object];
}
if ($object instanceof stdClass) {
return (array) $object;
}
$res = [];
$reflectionClass = new \ReflectionClass($object);
$properties = $reflectionClass->getProperties();
foreach ($properties as $prop) {
/* @var $prop ReflectionProperty */
$name = $prop->getName();
if ($name[0] == "_") {
continue;
}
$getter = "get" . ucfirst(lcfirst(strtr(ucwords(strtr($name, ['_' => ' '])), [' ' => ''])));
if (is_callable([$object, $getter])) {
$getter_value = $object->{$getter}();
if ($recursive && (is_array($getter_value) || is_object($getter_value))) {
$res[$name] = ArrayHelper::toArray($getter_value, $recursive);
} else {
$res[$name] = $getter_value;
}
}
}
return $res;
}