如果存在于数组(php)中

Hap*_*ppy 2 php arrays

有两个数组,$link来自foreach.一次$link必须是第一个箭头,第三个 - 第二个.所以:

1个数组:

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] => ) ) 
Run Code Online (Sandbox Code Playgroud)

2阵列:

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] => )
)
Run Code Online (Sandbox Code Playgroud)

差异 - 第一个有[sizes].

寻找一种方法来检测,是有[sizes]在给定的数组.

尝试过if (in_array("[sizes]", $link)) { } else { },但它不起作用.

谢谢.

cod*_*ict 6

既然sizes是一个数组键,你可以使用函数array_key_exists,TRUE如果在key中设置了给定,则返回该函数array.

if(array_key_exists('sizes',$link)) {
  // sizes exists
}else{
  // sizes does not exist.
}
Run Code Online (Sandbox Code Playgroud)

  • @Glister:函数名中有拼写错误.它应该是**array_key_exists**.这是**工作示例**:http://www.ideone.com/jH9OZ (2认同)