簡體   English   中英

如何從PHP中的多維數組中獲取特定鍵的值

[英]How to get value of a specific key from multidimensional array in PHP

我從 postmeta 表中的 post id 中獲取圖像

$kbe_img_meta = get_post_meta($foo_img_ID, "_wp_attachment_metadata", true);

echo "<pre>";
       print_r($foo_img_meta);
echo "</pre>";

這是我的多維數組結果:

    Array
    (
        [width] => 800
        [height] => 640
        [file] => 2014/09/Wallpaper_29.jpg
        [sizes] => Array
            (
                [thumbnail] => Array
                    (
                        [file] => Wallpaper_29-150x150.jpg
                        [width] => 150
                        [height] => 150
                        [mime-type] => image/jpeg
                    )

                [medium] => Array
                    (
                        [file] => Wallpaper_29-300x240.jpg
                        [width] => 300
                        [height] => 240
                        [mime-type] => image/jpeg
                    )

                [post-thumbnail] => Array
                    (
                        [file] => Wallpaper_29-624x499.jpg
                        [width] => 624
                        [height] => 499
                        [mime-type] => image/jpeg
                    )

            )

        [image_meta] => Array
            (
                [aperture] => 0
                [credit] => 
                [camera] => 
                [caption] => 
                [created_timestamp] => 0
                [copyright] => 
                [focal_length] => 0
                [iso] => 0
                [shutter_speed] => 0
                [title] => 
                [orientation] => 1
            )

    )
Array
(
    [width] => 800
    [height] => 480
    [file] => 2014/09/Wallpaper_37.jpg
    [sizes] => Array
        (
            [thumbnail] => Array
                (
                    [file] => Wallpaper_37-150x150.jpg
                    [width] => 150
                    [height] => 150
                    [mime-type] => image/jpeg
                )

            [medium] => Array
                (
                    [file] => Wallpaper_37-300x180.jpg
                    [width] => 300
                    [height] => 180
                    [mime-type] => image/jpeg
                )

            [post-thumbnail] => Array
                (
                    [file] => Wallpaper_37-624x374.jpg
                    [width] => 624
                    [height] => 374
                    [mime-type] => image/jpeg
                )

        )

    [image_meta] => Array
        (
            [aperture] => 0
            [credit] => 
            [camera] => 
            [caption] => 
            [created_timestamp] => 0
            [copyright] => 
            [focal_length] => 0
            [iso] => 0
            [shutter_speed] => 0
            [title] => 
            [orientation] => 1
        )

)

    Array
    (
        [width] => 800
        [height] => 640
        [file] => 2014/09/Wallpaper_33.jpg
        [sizes] => Array
            (
                [thumbnail] => Array
                    (
                        [file] => Wallpaper_33-150x150.jpg
                        [width] => 150
                        [height] => 150
                        [mime-type] => image/jpeg
                    )

                [medium] => Array
                    (
                        [file] => Wallpaper_33-300x240.jpg
                        [width] => 300
                        [height] => 240
                        [mime-type] => image/jpeg
                    )

                [post-thumbnail] => Array
                    (
                        [file] => Wallpaper_33-624x499.jpg
                        [width] => 624
                        [height] => 499
                        [mime-type] => image/jpeg
                    )

            )

        [image_meta] => Array
            (
                [aperture] => 0
                [credit] => 
                [camera] => 
                [caption] => 
                [created_timestamp] => 0
                [copyright] => 
                [focal_length] => 0
                [iso] => 0
                [shutter_speed] => 0
                [title] => 
                [orientation] => 1
            )

    )

現在我只想要來自[thumbnail][medium][post-thumbnail] [file]值,所以我從文件夾中刪除了圖像。

注意:我只想要 [file] 值。

您可以使用以下語法:

$file1 = $foo_img_meta['sizes']['thumbnail']['file']; 
$file2 = $foo_img_meta['sizes']['medium']['file']; 
$file3 = $foo_img_meta['sizes']['post-thumbnail']['file']; 

echo $file1." _thumbnail"."<br />"; 
echo $file2." _medium"."<br />"; 
echo $file3." _post-thumbnail"."<br />";}

如果您不知道將遇到的尺寸,請改用以下內容:

foreach ($result['sizes'] as $size_id => $size) {
    $file = $size['file'];
    // Delete File
}

暫無
暫無

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

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