php - 多维数组转对象,具体方式

标签 php arrays object multidimensional-array

我知道这类问题已经被问了很多,但我还没有找到如何按照我想要的方式去做。

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

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;

如果可能的话,我有什么办法可以做到这一点吗?

最佳答案

您需要为数组值实例化一个新对象以设置属性。除非您有更具体的内容,否则 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;
}

关于php - 多维数组转对象,具体方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8410300/

相关文章:

php - 在 Laravel 中使用 where、whereHas 执行查询

php - MySQL 连接两个表的多条记录

arrays - 为什么这个简单的数组访问在 Swift 中不起作用?

Python:为什么这个数组是一个对象而不是 float ?

java - 使用另一个类创建新对象

php - 安装 PHPUnit 'unknown channel "时出错 pear.phpunit.de"in "pear.phpunit.de/PHPUnit"'

php - 如何更改 Mediawiki 1.19.1 的登录页面以直接转到特殊 :UserLogin

javascript - 如何打印数组中每个单词之间的空格

c++ - x++ 用于 C++ 中的对象

php - 调用的子常量在父级的静态函数中不可用