检查数组键是否为空

时间:2014-04-01 10:06:54

标签: php arrays

我循环浏览foreach,如果值不是空字符串,我只想执行一些代码。有多个键只有一个空字符串作为属性。

我有以下内容:

<?php foreach ( $images as $x ):
        if ( $x['description'] != '' ):  
?>
        <p class="description"><?php echo $x['description'] ?></p>
<?php else:
          echo 'else!';
    endif;
endforeach; ?>

然而,它正在做两件事:执行ifelse

这是我循环的数组:

ARRAY (SIZE=7)
  0 => 
    ARRAY (SIZE=10)
      'ID' => INT 111
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'THIS IS THE TITLE' (LENGTH=17)
      'CAPTION' => STRING 'HTTPS://SOUNDCLOUD.COM/LATENIGHTTALES/LNT-BNB-ALBUM-MINI-MIX' (LENGTH=60)
      'DESCRIPTION' => STRING 'THIS IS THE DESCRIPTION FOR THIS IMAGE. ' (LENGTH=40)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/BEAUTY.PNG' (LENGTH=63)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/BEAUTY.PNG' (LENGTH=63)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/BEAUTY.PNG' (LENGTH=63)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/BEAUTY.PNG' (LENGTH=63)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  1 => 
    ARRAY (SIZE=10)
      'ID' => INT 154
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'PLACEHOLDER' (LENGTH=11)
      'CAPTION' => STRING 'CAPTION2' (LENGTH=8)
      'DESCRIPTION' => STRING 'DESCRIPTION2' (LENGTH=12)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER.PNG' (LENGTH=68)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER.PNG' (LENGTH=68)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER.PNG' (LENGTH=68)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER.PNG' (LENGTH=68)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  2 => 
    ARRAY (SIZE=10)
      'ID' => INT 155
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'TITLE3' (LENGTH=6)
      'CAPTION' => STRING 'CAPTION3' (LENGTH=8)
      'DESCRIPTION' => STRING 'DESCRIPTION 3' (LENGTH=13)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER1.PNG' (LENGTH=69)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER1.PNG' (LENGTH=69)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER1.PNG' (LENGTH=69)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER1.PNG' (LENGTH=69)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  3 => 
    ARRAY (SIZE=10)
      'ID' => INT 156
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'PLACEHOLDER' (LENGTH=11)
      'CAPTION' => STRING '' (LENGTH=0)
      'DESCRIPTION' => STRING '' (LENGTH=0)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER2.PNG' (LENGTH=69)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER2.PNG' (LENGTH=69)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER2.PNG' (LENGTH=69)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER2.PNG' (LENGTH=69)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  4 => 
    ARRAY (SIZE=10)
      'ID' => INT 157
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'PLACEHOLDER' (LENGTH=11)
      'CAPTION' => STRING '' (LENGTH=0)
      'DESCRIPTION' => STRING '' (LENGTH=0)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER3.PNG' (LENGTH=69)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER3.PNG' (LENGTH=69)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER3.PNG' (LENGTH=69)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER3.PNG' (LENGTH=69)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  5 => 
    ARRAY (SIZE=10)
      'ID' => INT 158
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'PLACEHOLDER' (LENGTH=11)
      'CAPTION' => STRING '' (LENGTH=0)
      'DESCRIPTION' => STRING '' (LENGTH=0)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER4.PNG' (LENGTH=69)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER4.PNG' (LENGTH=69)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER4.PNG' (LENGTH=69)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER4.PNG' (LENGTH=69)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400
  6 => 
    ARRAY (SIZE=10)
      'ID' => INT 159
      'ALT' => STRING '' (LENGTH=0)
      'TITLE' => STRING 'PLACEHOLDER' (LENGTH=11)
      'CAPTION' => STRING '' (LENGTH=0)
      'DESCRIPTION' => STRING '' (LENGTH=0)
      'MIME_TYPE' => STRING 'IMAGE/PNG' (LENGTH=9)
      'URL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER5.PNG' (LENGTH=69)
      'WIDTH' => INT 300
      'HEIGHT' => INT 400
      'SIZES' => 
        ARRAY (SIZE=9)
          'THUMBNAIL' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER5.PNG' (LENGTH=69)
          'THUMBNAIL-WIDTH' => INT 72
          'THUMBNAIL-HEIGHT' => INT 96
          'MEDIUM' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER5.PNG' (LENGTH=69)
          'MEDIUM-WIDTH' => INT 300
          'MEDIUM-HEIGHT' => INT 400
          'LARGE' => STRING 'HTTP://FASHION-DETAIL.DEV/WP-CONTENT/UPLOADS/2014/03/PLACEHOLDER5.PNG' (LENGTH=69)
          'LARGE-WIDTH' => INT 300
          'LARGE-HEIGHT' => INT 400

3 个答案:

答案 0 :(得分:0)

你的意思是:

  

执行if和else。

您是否同时看到description和'其他!'对于相同的数组行? (不知道这是怎么可能的)

或者你的意思是在检测到空值时不应该解析父数组元素?现在,您将看到不同数组行(但是一个父数组行)的if else语句结果

请您以这种方式修改代码(并显示部分结果):

<?php
   foreach ( $images as $index => $x ):
        if ( $x['description'] != '' ):  
?>
            <p class="description">
                <?php echo $index . ':' . $x['description'] ?>
            </p>
<?php   else: echo $index . 'else!';
    endif;
endforeach;?>

答案 1 :(得分:0)

试试这个:

<?php foreach ( $images as $x ) {
    if ( !empty($x['description'])) {
       ?><p class="description"><?php echo $x['description'] ?></p><?php
    }else{
       echo 'else!';
    }
} ?>

答案 2 :(得分:0)

你可以在字符串情况下使用trim来删除分隔线并在比较之前清空espaces