将多维数组展平为单个数组,使用“点”表示法指示深度

时间:2016-10-22 08:58:34

标签: php arrays iterator

我尝试编写一个解决方案,用于在PHP 5.6 +上转换多维数组(更多2个嵌套)

尝试使用RecursiveIteratorIterator,但我不太了解,也不能正常工作。

//dummyname = dummyname + num;
//String x = sp.getString(dummyname, "not found");
String newDummy= dummyname + num;
String x = sp.getString(newDummy, "not found");

我喜欢它:

array:3 [
  "app" => array:3 [
    "cp" => array:3 [
      "production" => true
      "panel" => "cpanel"
      "home" => "/home"
    ]
    "rsync" => array:4 [
      "binary" => "/usr/bin/rsync"
      "options" => " -azhv --inplace --delete"
      "options_extra" => " -azvh --checksum --no-times --no-o --no-g --inplace --delete"
      "local" => "/cprsync_back"
    ]
    "remotes" => array:1 [
      "master" => array:4 [
        "ip" => "1.1.1.1"
        "user" => "myuser"
        "port" => "myport"
        "path" => "/Volumes/MACBACKUP/cprsync_remote/hq"
      ]
    ]
  ]
  "app" => array:5 [
    "name" => "CpRsync"
    "log_level" => "debug"
    "timezone" => "Europe/Madrid"
    "locale" => "en"
    "fallback_locale" => "en"
  ]
  "mail" => array:6 [
    "driver" => "smtp"
    "host" => "smtp.host.com"
    "port" => "26"
    "encryption" => "tls"
    "username" => "userpop@host.com"
    "password" => "z4t3@#w2Qwa8OM"
  ]
]

对我来说最好的方式是

array:22 [
   "app.cp.producction" => true
   "app.cp.panel" => "cpanel"
   ...
   "remotes.master.ip" => "1.1.1.1"
   "remotes.master.user" => "myuser" 
   ...
]

代码工作,但是,这是正确的方法吗?最好的油漆代码可能吗?

0 个答案:

没有答案