So I have a function which loops through Objects recursive, cause some of Object have objects inside for example the object Person can contain object Adress and puts them into an array.
so basically this can be a structure from a model:
[obj] Neededforexample
- [obj] person
-- [prop] first-name
-- [obj] adress
- SomeProperty
So the function which loops through it is completed:
private static function SetPropertiesArray($class,$parentClass = null){
$object = new $class;
$objectProperties = get_object_vars($object);
foreach($objectProperties as $prop => $value){
//echo $prop;
if(class_exists($prop)){
if($parentClass !== null){
self::$_propertiesArray[$parentClass][$prop] = $value;
}
else{
self::$_propertiesArray[$prop] = $value;
}
self::SetPropertiesArray($prop,$prop);
}
else{
if($parentClass !== null){
self::$_propertiesArray[$parentClass][$prop] = $value;
}
else{
self::$_propertiesArray[$prop] = $value;
}
}
}
return self::$_propertiesArray;
}
This outputs this array:
"NeededForExample" => array(
"Person" => null,
"SomeProperty" => "Somevalue",
"First-name" => "Firstname",
"adress" => "HERE")
While I want:
"NeededForExample" => array(
"Person" => array(
"First-name" => "firstname",
"adress" => array(
"street" => "streetname"
)
),
"SomeProperty" => "Somevalue")
var_dump()on your data and copy-paste the first 1 or 2 elements....if(class_exists($prop)){; should it not readif(class_exists($class)){? Because$propseems like the Property of the$classin question and not the Class itself... andget_object_varsreturns an Array containing the properties of an Object... but not the Class itself....