【问题标题】:Multidimensional array to object, specific way多维数组转对象,具体方式
【发布时间】:2012-01-14 16:02:18
【问题描述】:

我知道这种类型的问题被问了很多,但我还没有找到我想要的方法。

所以,基本上我有这个数组。

array(7) {
  ["site"]=>
  array(5) {
    ["production"]=>
    bool(false)
    ["url"]=>
    string(29) "http://localhost/"
    ["name"]=>
    string(6) "Sitename"
    ["title"]=>
    string(7) ": Index"
    ["pagedata"]=>
    array(1) {
      ["default"]=>
      string(5) "Index"
    }
  }
  ["DB"]=>
  array(5) {
    ["host"]=>
    string(9) "localhost"
    ["user"]=>
    string(4) "root"
    ["pass"]=>
    string(4) "secret"
    ["database"]=>
    string(12) "database"
    ["engine"]=>
    string(7) "eMySQLi"
  }
  ["cache"]=>
  array(2) {
    ["file"]=>
    array(1) {
      ["time"]=>
      int(500)
    }
    ["memcache"]=>
    array(2) {
      ["my_first_vps_ip"]=>
      string(17) "my_first_vps_port"
      ["my_second_vps_ip"]=>
      string(18) "my_second_vps_port"
    }
  }
  ["skin"]=>
  array(2) {
    ["name"]=>
    string(9) "thehabbos"
    ["mobile"]=>
    array(2) {
      ["enabled"]=>
      bool(true)
      ["name"]=>
      string(16) "mobile_thehabbos"
    }
  }
  ["lang"]=>
  array(1) {
    ["name"]=>
    string(7) "English"
  }
  ["widget"]=>
  array(1) {
    ["default"]=>
    string(7) "Icecron"
  }
  ["cron"]=>
  array(1) {
    ["DatabaseBackup"]=>
    array(1) {
      ["execute_every"]=>
      int(86400)
    }
  }
}

所以,如果我用这样的方法“解析”这个数组(将数组转换为对象)..

    private function parse($arr)
    {
        foreach ($arr as $key => $val) 
        {
            $this->{$key} = is_array($val) ? $this->parse($val) : $val;
        }
        return $this;
    }

我最终会得到类似......

object(Configure)#3 (27) {
  ["production"]=>
  bool(false)
  ["url"]=>
  string(29) "http://localhost/RevFramework"
  ["name"]=>
  string(7) "English"
  ["title"]=>
  string(7) ": Index"
  ["default"]=>
  string(7) "Icecron"
  ["pagedata"]=>
  *RECURSION*
  ["site"]=>
  *RECURSION*
  ["host"]=>
  string(9) "localhost"
  ["user"]=>
  string(4) "root"
  ["pass"]=>
  string(4) "root"
  ["database"]=>
  string(12) "rev_database"
  ["engine"]=>
  string(7) "eMySQLi"
  ["DB"]=>
  *RECURSION*
  ["time"]=>
  int(500)
  ["file"]=>
  *RECURSION*
  ["my_first_vps_ip"]=>
  string(17) "my_first_vps_port"
  ["my_second_vps_ip"]=>
  string(18) "my_second_vps_port"
  ["memcache"]=>
  *RECURSION*
  ["cache"]=>
  *RECURSION*
  ["enabled"]=>
  bool(true)
  ["mobile"]=>
  *RECURSION*
  ["skin"]=>
  *RECURSION*
  ["lang"]=>
  *RECURSION*
  ["widget"]=>
  *RECURSION*
  ["execute_every"]=>
  int(86400)
  ["DatabaseBackup"]=>
  *RECURSION*
  ["cron"]=>
  *RECURSION*
}

所以,我可以这样使用它..

echo $this->url;

但我想做的就是这样使用它..

echo $this->site->url;

如果可能的话,我知道如何实现这一点吗?

【问题讨论】:

    标签: php arrays object multidimensional-array


    【解决方案1】:

    您需要为数组值实例化一个新对象以设置属性。除非您有更具体的内容,否则 stdClass 会做:

    private function parse(array $arr, stdClass $parent = null) {
        if ($parent === null) {
            $parent = $this;
        }
    
        foreach ($arr as $key => $val) {
            if (is_array($val)) {
                $parent->$key = $this->parse($val, new stdClass);
            } else {
                $parent->$key = $val;
            }
        }
    
        return $parent;
    }
    

    【讨论】:

    • 这是我准备的示例,展示了使用stdClass的两种不同方式:codepad.org/mkG1W320
    • 也非常感谢!将尝试对这两种方法进行基准测试,看看我会选择哪一种!
    • @Manuel json_decode 隐式创建stdClass 的实例。如果您想要一个对象,该对象需要是某个类的实例。 stdClass 是在这种情况下使用的默认“虚拟”类。 JSON 方法是一个不错的技巧,但请注意,它无法处理某些类型的变量(如资源),这些变量在编码/解码过程中会丢失。
    • 哦,我明白了,谢谢。无论如何,让它完美地工作。再次感谢大家。
    【解决方案2】:

    我认为这会做你想要的:

    $obj = json_decode(json_encode($my_array));
    echo $obj->site->url;
    

    【讨论】:

    • 正是我想要的,唯一的问题是我不能将它分配给 $this,但没关系。非常感谢!
    猜你喜欢
    • 2012-07-23
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2016-11-28
    • 2012-11-14
    相关资源
    最近更新 更多