简体   繁体   中英

If key has value

I have an array, $data, that looks like this when printed:

[1] => Array 
    [type] => link
[2] => Array
    [type] => photo
[3] => Array
    [type] => video

I have a foreach statement for the $data, so each $data holds a [type] key. I need to be able to check if the [type] key has a value of 'link', else a value of 'photo', else a value of 'video'.

Any help would be great. I tried array_key_exists, but that is just to check if a key is present in the data string.

foreach ($data as $datum) {
   switch ($datum['type']) {
      case 'link':
         //fill in
         break;
      case 'photo':
         //fill in
         break;
      case 'video':
         //fill in
         break;
      default:
   }
}
foreach ($data as $v) {

    switch ($v['type']) {
    case 'link':
        echo "it's a link";
        break;
    case 'photo':
        echo "it's a photo";
        break;
    case 'video:
        echo "it's a video";
        break;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM