简体   繁体   English

如何使用 PHP 检索多维数组的特定元素

[英]How to retrieve specific elements of a multi-dimensional array using PHP

I am building a homelab dashboard using php and html.我正在使用 php 和 html 构建一个 homelab 仪表板。 Currently I am attempting to take information from an array and display it within a div.目前我正在尝试从数组中获取信息并将其显示在 div 中。 The information provided in the array is the url for an iframe as well as height and width adjustements.数组中提供的信息是 iframe 的 url 以及高度和宽度调整。 My question is how would I retrieve specific elements of this array so I can display it correctly.我的问题是如何检索该数组的特定元素,以便正确显示它。

$dl = [
    'dashlet1' => [
        'url' => 'some random url',
        'height' => 'height=200',
        'width' => 'width=450'
    ],
    'dashlet2' => [
        'url' => 'another random url',
        'height' => 'height=200',
        "width" => 'width=450'
    ],
    'dashlet3' => [
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=450'
    ],
    'dashlet4' => [
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=350'
    ],
    'dashlet5' => [
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=450'
    ],
    'dashlet6' => [
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=450'
    ],
    'dashlet7' => [
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=350'
    ],
    'dashlet8' =>[
        'url' => 'another random url',
        'height' => 'height=200',
        'width' => 'width=450'
    ]

];   

Lets say you want to access the url.假设您要访问该网址。 You can do this by doing你可以这样做

echo $dl['dashlet1']['url'];
echo $dl['dashlet2']['url'];

etc.等等。

You can also use a loop.您也可以使用循环。

    foreach( $dl as $key => $value ) {

        echo($key . " " . $value['url']);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM