首页 > 解决方案 > 从嵌套数组中获取 2 个值

问题描述

我有一个数组,里面有 2 个数组。我想从嵌套数组中回显 2 个值。

值键是:

[文档根目录]

[领域]

这是我的数组:

Array (
     [0] => Array (
            [all_aliases_valid] => 
            [usecanonicalname] => Off
            [owner] => root
            [documentroot] => /home/oneclickinstalle/public_html/test
            [group] => oneclickinstalle
            [can_https_redirect] => 
            [ip] => 172.105.97.228
            [hascgi] => 1
            [type] => sub_domain
            [homedir] => /home/oneclickinstalle
            [ipv6] => 
            [no_cache_update] => 0
            [servername] => test.oneclickinstaller.io
            [serveradmin] => webmaster@test.oneclickinstaller.io
            [phpopenbasedirprotect] => 1
            [serveralias] => www.test.oneclickinstaller.io
            [userdirprotect] => 
            [user] => oneclickinstalle
            [domain] => test.oneclickinstaller.io
            [is_https_redirecting] => 
            [status] => not redirected
        )

    [1] => Array (
            [domain] => demo.oneclickinstaller.io
            [user] => oneclickinstalle
            [is_https_redirecting] => 
            [status] => not redirected
            [userdirprotect] => 
            [serveralias] => www.demo.oneclickinstaller.io
            [serveradmin] => webmaster@demo.oneclickinstaller.io
            [phpopenbasedirprotect] => 1
            [servername] => demo.oneclickinstaller.io
            [ipv6] => 
            [no_cache_update] => 0
            [hascgi] => 1
            [homedir] => /home/oneclickinstalle
            [type] => sub_domain
            [ip] => 172.105.97.228
            [group] => oneclickinstalle
            [can_https_redirect] => 
            [documentroot] => /home/oneclickinstalle/public_html/demo
            [owner] => root
            [all_aliases_valid] => 
            [usecanonicalname] => Off
        )

)

我设法让它工作,但只是为了域名,我无法同时获得 [documentroot] 值。

这是我的循环,

foreach ( $wp_sub_domains_array as $array => $sub_domain ) {
  foreach( $sub_domain as $key => $sub_domain_name ) {
    if( $key == "domain" ) {
        echo "<option value='" . $sub_domain_name . "'>" . $sub_domain_name . "</option>";
     }
  }
}

我想在域名旁边回显 [documentroot] 值,像这样,

foreach ( $wp_sub_domains_array as $array => $sub_domain ) {
  foreach( $sub_domain as $key => $sub_domain_name ) {
    if( $key == "domain" ) {
        echo "<option value='" . $sub_domain_name . "|" . $sub_domain_documentroot . "'>" . $sub_domain_name . "</option>";
     }
  }
}

我是一个新手,显然做错了什么,或者我不理解一些基本的东西。任何见解都会有所帮助和赞赏。

标签: phploopsforeach

解决方案


您只需要一个循环即可遍历所有子域信息数组,然后您可以直接从中访问信息。

foreach ( $wp_sub_domains_array as $array => $sub_domain ) {
   echo "<option value='" . $sub_domain['domain'] . "|" . $sub_domain['documentroot'] . "'>" . $sub_domain['domain'] . "</option>";
}

推荐阅读