簡體   English   中英

如何使用 PHP 檢索多維數組的特定元素

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

我正在使用 php 和 html 構建一個 homelab 儀表板。 目前我正在嘗試從數組中獲取信息並將其顯示在 div 中。 數組中提供的信息是 iframe 的 url 以及高度和寬度調整。 我的問題是如何檢索該數組的特定元素,以便正確顯示它。

$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'
    ]

];   

假設您要訪問該網址。 你可以這樣做

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

等等。

您也可以使用循環。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM