dongliao3742 2014-10-17 06:28
浏览 42
已采纳

如何从PHP中的多维数组中获取特定键的值

I get images from its post id in postmeta table

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

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

Here is my multidimensional array result:

    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
            )

    )

Now I only want [file] value from [thumbnail], [medium] and [post-thumbnail], so i delete images from folder.

Note: I only want [file] value.

  • 写回答

1条回答 默认 最新

  • dongyanpai2701 2014-10-17 06:31
    关注

    You can use the following syntax:

    $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 />";}
    

    In case you are not aware of the sizes you'll be encountering, use the following instead:

    foreach ($result['sizes'] as $size_id => $size) {
        $file = $size['file'];
        // Delete File
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号