需要帮助的人,下面是一个var_dump数组,我在php中非常菜鸟,我对如何做到这一点感到很困惑,基本上我只需要一个条件
$fittin_colour_image = get_field('featured_images', get_the_ID());
以上是使用ACF的代码
var_dump($fittin_colour_image);
下面是结果
array (size=2)
0 =>
array (size=3)
'fitting_colour' =>
array (size=1)
0 => string 'Black' (length=5)
'image' => string 'http://localhost/mysite.com/wp-content/uploads/2018/04/image-black.jpg' (length=101)
'image_description' => string '' (length=0)
1 =>
array (size=3)
'fitting_colour' =>
array (size=1)
0 => string 'White' (length=5)
'image' => string 'http://localhost/mysite.com/wp-content/uploads/2018/05/image-white.png' (length=100)
'image_description' => string '' (length=0)
我想遇到这样的情况
如果此数组的颜色为“黑色”,则显示其图像
我当前正在使用此代码
if ($fittin_colour_image[0]['fitting_colour'][0] == 'Black') {
echo $fittin_colour_image[0]['image'];
}
有点麻烦,因为每次我更改条件时都更改[0],对不起,但是我现在很困惑,不知道该说些什么,请帮忙
答案 0 :(得分:0)
您非常亲密。无需对索引进行硬编码(在您的情况下为0
),而是可以遍历数组并显示图像:
foreach ($fittin_colour_image as $image) {
if ($image['fitting_colour'][0] == 'Black') {
echo $image['image'];
}
}
答案 1 :(得分:0)
测试
$fittin_colour_image = get_field('featured_images', get_the_ID());
if(!empty($fittin_colour_image)){ // Test if array not empty
foreach ($fittin_colour_image as $image) {
if(!empty($image['fitting_colour'][0]) && $image['fitting_colour'][0] === 'Black'){
echo '<img src="'.$image['image'].'" alt="'.$image['image_description'].'" />';
}
}
}