如果存在于数组(php)中

时间:2010-04-28 16:55:55

标签: php arrays

有两个数组,来自$link的{​​{1}}。一次foreach必须是第一个箭头,第三个 - 第二个。所以:

1阵列:

$link

2阵列:

Array (

[width] => 800

[height] => 1142

[hwstring_small] => height='96' width='67' [file] => 2010/04/white-1051279.jpg

[sizes] => Array (
[thumbnail] => Array ( [file] => white-1051279-100x150.jpg [width] => 100 [height] => 150 )
[medium] => Array ( [file] => white-1051279-200x285.jpg [width] => 200 [height] => 285 )
)

[image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0

[copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => ) ) 

区别 - 第一个有Array ( [width] => 50 [height] => 50 [hwstring_small] => height='50' width='50' [file] => 2010/04/images1.jpeg [image_meta] => Array ( [aperture] => 0 [credit] => [camera] => [caption] => [created_timestamp] => 0 [copyright] => [focal_length] => 0 [iso] => 0 [shutter_speed] => 0 [title] => ) )

正在寻找一种检测方式,在给定数组中有[sizes]

尝试[sizes],但它不起作用。

感谢。

3 个答案:

答案 0 :(得分:6)

由于sizes是一个数组键,因此如果在TRUE中设置了给定的key,则可以使用函数array_key_exists,该函数返回array

if(array_key_exists('sizes',$link)) {
  // sizes exists
}else{
  // sizes does not exist.
}

答案 1 :(得分:3)

if (isset($theArray['sizes'])) {...}

答案 2 :(得分:3)

if(isset($link['sizes'])) {

}

这是你要找的吗?