访问数组中的特定字段

时间:2021-08-09 作者:user3787062

如何仅从此数组中提取图像url。

     <?php 
     
     $rec1= get_post_meta($post->ID, \'fave_recommended-property-1\', true);
     $rec2= get_post_meta($post->ID, \'fave_recommended-property-2\', true);
     $rec3= get_post_meta($post->ID, \'fave_recommended-property-3\', true);
     $rec4= get_post_meta($post->ID, \'fave_recommended-property-4\', true);
     $rec5= get_post_meta($post->ID, \'fave_recommended-property-5\', true);
     
     $rec1_vals = get_post_meta($rec1);
    
    echo $rec1_vals[\'fave_property_price\'][0] . \' \' .  $rec1_vals[\'fave_property_price_postfix\'][0]; echo \'<br>\';
    echo $rec1_vals[\'fave_property_bedrooms\'][0] . \' BHK\';
    
    $size = \'houzez-item-image-1\';
    $properties_images = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec1_vals[\'fave_property_id\'][0] );
在数组$Property\\u images中:我只希望在html图像中显示[介质]下的图像url。每个变量$rec1$rec5具有相同的重复设置。无法仅访问阵列的这一部分。非常感谢您的帮助。。

数组输出非常大,我在这里简短地粘贴了。。。

Array
(
    [19991] => Array
        (
            [width] => 592
            [height] => 444
            [file] => 2021/07/1-88-scaled.jpg
            [sizes] => Array
                (
                    [medium] => Array
                        (
                            [file] => 1-88-300x211.jpg
                            [width] => 300
                            [height] => 211
                            [mime-type] => image/jpeg
                            [url] => https://doorsandshelters.com/wp-content/uploads/2021/07/1-88-300x211.jpg
                        )

                    [large] => Array
                        (
                            [file] => 1-88-1024x721.jpg
                            [width] => 1024
                            [height] => 721
                            [mime-type] => image/jpeg
                            [url] => https://doorsandshelters.com/wp-content/uploads/2021/07/1-88-1024x721.jpg
                        )

                    [thumbnail] => Array
                        (
                            [file] => 1-88-150x150.jpg
                            [width] => 150
                            [height] => 150
                            [mime-type] => image/jpeg
                            [url] => https://doorsandshelters.com/wp-content/uploads/2021/07/1-88-150x150.jpg
                        )

                    [medium_large] => Array
                        (
                            [file] => 1-88-768x540.jpg
                            [width] => 768
                            [height] => 540
                            [mime-type] => image/jpeg
                            [url] => https://doorsandshelters.com/wp-content/uploads/2021/07/1-88-768x540.jpg
                        )
终于找到了答案。谢谢@Buttered\\u Toast!

 $rec1= get_post_meta($post->ID, \'fave_recommended-property-1\', true);
 $rec2= get_post_meta($post->ID, \'fave_recommended-property-2\', true);
 $rec3= get_post_meta($post->ID, \'fave_recommended-property-3\', true);
 $rec4= get_post_meta($post->ID, \'fave_recommended-property-4\', true);
 $rec5= get_post_meta($post->ID, \'fave_recommended-property-5\', true);
 
 $rec1_vals = get_post_meta($rec1);
 $rec2_vals = get_post_meta($rec2);
 $rec3_vals = get_post_meta($rec3);
 $rec4_vals = get_post_meta($rec4);
 $rec5_vals = get_post_meta($rec5);
 
 $size = \'houzez-item-image-1\';

$properties_images_rec1 = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec1_vals[\'fave_property_id\'][0] );
$properties_images_rec2 = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec2_vals[\'fave_property_id\'][0] );
$properties_images_rec3 = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec3_vals[\'fave_property_id\'][0] );
$properties_images_rec4 = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec4_vals[\'fave_property_id\'][0] );
$properties_images_rec5 = rwmb_meta( \'fave_property_images\', \'type=plupload_image&size=\'.$size, $rec5_vals[\'fave_property_id\'][0] );
       

    get_images_medium_urls($medium_images_urls1, $properties_images_rec1);
    get_images_medium_urls($medium_images_urls2, $properties_images_rec2);
    get_images_medium_urls($medium_images_urls3, $properties_images_rec3);
    get_images_medium_urls($medium_images_urls4, $properties_images_rec4);
    get_images_medium_urls($medium_images_urls5, $properties_images_rec5);

1 个回复
最合适的回答,由SO网友:Buttered_Toast 整理而成

根据你的阵列,你可以这样做。

这是仅获取中等大小url所需的最小代码

处理查找所有媒体URL的函数

function get_images_medium_urls (&$urls = [], $arr, $find = [\'medium\', \'url\']) {
    foreach ($arr as $key => $value) {
        if ($key === $find[0]) $urls[] = $value[$find[1]];
        else if (is_array($value)) get_images_medium_urls($urls, $value, $find);
    }
}
您的变量是$properties_images 因此,使用函数将如下所示

get_images_medium_urls($medium_images_urls, $properties_images);
在调用函数之后,因为我们传递的第一个参数是$medium_images_urls, 它将包含所有找到的媒体url,如下所示

Array
(
    [0] => https://doorsandshelters.com/wp-content/uploads/2021/07/1-88-300x211.jpg
)
例如,如果您将来想获取大型图像的URL,可以这样做

get_images_medium_urls($medium_images_urls, $properties_images, [\'large\', \'url\']);
是的,如果您使用函数名来获取媒体图像URL以外的任何内容,那么函数名可能会令人困惑,但这只是一个示例,您可以随意更改它。