php: convert an object to array
2019-11-09 00:00:00
IN1
,
2023-04-10 14:16:30
IN1
Q: how to convert an object to array with php?
A: use the following recursive function convertObjectToArray
to achieve this.
/**
* @param mixed $mObject
* @return array
*/
function convertObjectToArray($mObject)
{
(is_object($mObject)) ? $mObject = (array) $mObject : false;
if(is_array($mObject))
{
$aNew = array();
foreach($mObject as $sKey => $mValue)
{
$sFirstChar = trim(substr(trim($sKey), 0, 1));
(('*' === $sFirstChar)) ? $sKey = trim(substr(trim($sKey), 1)) : false;
$aNew[$sKey] = convertObjectToArray($mValue);
}
}
else
{
$aNew = $mObject;
}
return $aNew;
}