0

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")
6
  • Could you post 1 or 2 elements in the original array containing your data/objects? This is so that we can see the Structure a bit.... You could do a var_dump() on your data and copy-paste the first 1 or 2 elements.... Commented Jul 21, 2016 at 12:02
  • {"Person":{"SurName":null,"LastName":null,"BurgerServiceNummer":null,"Address":null},"Address":{"Street":null,"ZipCode":null,"City":null,"Country":null},"Message":null} Commented Jul 21, 2016 at 12:12
  • 1
    The line if(class_exists($prop)){; should it not read if(class_exists($class)){ ? Because $prop seems like the Property of the $class in question and not the Class itself... and get_object_vars returns an Array containing the properties of an Object... but not the Class itself.... Commented Jul 21, 2016 at 12:16
  • @Poiz yeah you're right should be this works fine tho since every models class included needs an object in my case but what you just said is true. Commented Jul 21, 2016 at 13:33
  • Something was tried below... instead of using Recursive function, 2 functions were used instead... check it out & see how it plays out. You can check out the Demo here: eval.in/609466 Commented Jul 21, 2016 at 13:39

2 Answers 2

1

The Code below may be pertinent to your case. But you may test it out here first.

    <?php

        $helper         = new Helper("Person", null);
        var_dump($helper->getPropertiesArray());



        class Helper{

            protected $Message;
            protected static $_propertiesArray = array();

            public function __construct($class, $parentClass) {
                self::SetPropertiesArray($class, $parentClass);
            }

            private static function SetPropertiesArray($class, $parentClass = null){
                $object             = new $class;
                $objectProperties   = get_object_vars($object);

                foreach($objectProperties as $prop => $value){
                    if(!is_null($parentClass)){
                        // CHECK IF $prop IS A CLASS...
                        if(class_exists($prop)){
                            self::$_propertiesArray[$parentClass][$class][$prop] = self::getClassPropertiesArray($prop, $parentClass);
                        }else{
                            self::$_propertiesArray[$parentClass][$class][$prop] = $value;
                        }
                    }else{
                        // CHECK IF $prop IS A CLASS...
                        if(class_exists($prop)){
                            self::$_propertiesArray[$class][$prop] = self::getClassPropertiesArray($prop, $parentClass);
                        }else{
                            self::$_propertiesArray[$class][$prop] = $value;
                        }

                    }
                }

                return self::$_propertiesArray;
            }

            private static function getClassPropertiesArray($class, $parentClass = null){
                $object             = new $class;
                $objectProperties   = get_object_vars($object);
                $arrClassProperties = array();

                foreach($objectProperties as $prop => $value){
                    $arrClassProperties[$prop] = $value;
                }
                return $arrClassProperties;
            }

            public function getPropertiesArray(){
                return self::$_propertiesArray;
            }
        }

        class Person{
            public $SurName  = "Test Surname";
            public $LastName = "BurgerServiceNummer" ;
            public $Address;
        }


        class Address{
            public $Street;
            public $ZipCode;
            public $City;
            public $Country;
        }

THE VAR_DUMP ABOVE PRODUCES::

        array (size=1)
          'Person' => 
            array (size=3)
              'SurName' => string 'Test Surname' (length=12)
              'LastName' => string 'BurgerServiceNummer' (length=19)
              'Address' => 
                array (size=4)
                  'Street' => null
                  'ZipCode' => null
                  'City' => null
                  'Country' => null
Sign up to request clarification or add additional context in comments.

Comments

0

This is my solution

<?php
function SearchArrayForKeyAndAdd(&$array, $given_key, $name, $data = null){
    foreach($array as $key => &$value){
        if(is_array($value)){
            if($key == $given_key){
                $value[$name] = $data;
            }
            else{
                SearchArrayForKeyAndAdd($value,$given_key, $name, $data);
            }

        }
        else{
            if($key == $given_key){
                $value[$name] = $data;
            }
        }
    }
}
$array = array(
    "person" => array(
        "Name" => "Roy Stijsiger",
        "Adress" => null
    )
);


SearchArrayForKeyAndAdd($array,"Adress","Huisnummer",1);

SearchArrayForKeyAndAdd($array,"Adress","Straatnaam","Nortierstraat");
var_dump($array);

This was dummy code^^ This is the working code like I showed above VV

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::SearchArrayForKeyAndAdd(self::$_propertiesArray,$parentClass,$prop,$value);
                }
                else{
                    self::$_propertiesArray[$prop] = $value;
                }
                self::SetPropertiesArray($prop,$prop);
            }
            else{
                if($parentClass !== null){
                    self::SearchArrayForKeyAndAdd(self::$_propertiesArray,$parentClass,$prop,$value);
                }
                else{
                    self::$_propertiesArray[$prop] = $value;
                }
            }
        }

        return self::$_propertiesArray;
    }

    private static function SearchArrayForKeyAndAdd(&$array, $given_key, $name, $data = null){
    foreach($array as $key => &$value){
        if(is_array($value)){
            if($key == $given_key){
                $value[$name] = $data;
            }
            else{
                self::SearchArrayForKeyAndAdd($value,$given_key, $name, $data);
            }
        }
        else{
            if($key == $given_key){
                $value[$name] = $data;
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.