php - 从嵌套数组中获取 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>";
}
}
}
我是一个新手,显然做错了什么,或者我不理解一些基本的东西。任何见解都会有所帮助和赞赏。
解决方案
您只需要一个循环即可遍历所有子域信息数组,然后您可以直接从中访问信息。
foreach ( $wp_sub_domains_array as $array => $sub_domain ) {
echo "<option value='" . $sub_domain['domain'] . "|" . $sub_domain['documentroot'] . "'>" . $sub_domain['domain'] . "</option>";
}
推荐阅读
- node.js - 无法获取 /post/api/posts/5c804ec6ad029f21201c686e 未找到,因为不需要附加单词“posts”
- python - 提供的令牌不是 Gitlab CI 的 UUID 错误
- android - 客户端重新建立丢失的连接后,保存将通过套接字从服务器发送到客户端的数据的正确方法是什么
- docker - libcontainer 在 docker 堆栈中的位置是什么?
- assembly - 在 mips 程序集中用循环写入数组
- bash - 如何 tar 目录中的所有 files.txt
- angular - 如何将组件类中的值传递给存在于类外部的函数
- spring-mvc - 依赖于其他 REST 服务的响应时,周围的建议不起作用
- sorting - 在工作表的一张纸上对特定范围进行排序的最佳方法是什么?
- r - (整洁,一目了然,增强)与 exec