按键查找数组中的数组,并设置值

时间:2016-07-21 11:59:55

标签: php arrays object recursion

所以我有一个循环遍历Objects的函数,导致一些Object内部有对象,例如对象 可以包含对象 地址并将它们放入数组中。

所以基本上这可以是模型中的结构:

 [obj] Neededforexample
 - [obj] person
 -- [prop] first-name
 -- [obj] adress
 - SomeProperty

所以循环它的函数完成了:

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;
}

输出此数组:

"NeededForExample" => array(
  "Person" => null,
  "SomeProperty" => "Somevalue",
  "First-name" => "Firstname",
  "adress" => "HERE")

虽然我想:

"NeededForExample" => array(
  "Person" => array(
   "First-name" => "firstname",
   "adress" => array(
     "street" => "streetname"
   ) 
  ),
  "SomeProperty" => "Somevalue")

2 个答案:

答案 0 :(得分:1)

以下“准则”可能与您的案件相关。但是你可以先用here来测试它。

    <?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;
        }

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

答案 1 :(得分:0)

这是我的解决方案

<?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);

这是虚拟代码^^ 这是我在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;
            }
        }
    }