2013-03-04 81 views
1

我试着用下面的代码检查数组是否存在,问题是当getFieldOrder('image_gal')里面没有图像时它会返回这个错误。如何在使用魔域时检查数组是否存在

错误输出

Warning: array_reverse() [function.array-reverse]: The argument should be an array in /home/sritamac/public_html/wp-content/plugins/magic-fields/get-custom.php on line 306 

Warning: sort() expects parameter 1 to be array, null given in /home/sritamac/public_html/wp-content/plugins/magic-fields/get-custom.php on line 307 

阵列码:

<?php 
//var 
$images = getFieldOrder('image_gal'); 

if (is_array($images)) { 

    foreach ($images as $image) { 

     if (get('image_gal', 1, $image) == TRUE) { //check if image_gallery_image has image 
?> 

    <div id="wrap"> 
     <ul id="mycarousel" class="jcarousel-skin-tango"> 
      <?php 
      $images = getFieldOrder('image_gal'); 
      foreach ($images as $image) { //loop image 
      ?> 
      <li> 
       <a class="group3" href="<?php echo get('image_gal', 1, $image);?>"> 
        <img src="<?php echo get('image_gal', 1, $image);?>" width="150" height="150" alt="" /> 
       </a> 
      </li> 
      <?php 
      } 
      ?> 
     </ul> 
    </div> 
<?php 
      break; 
     } 
    } 
} 
?> 

即时通讯使用这个主题http://www.s5themes.com/theme/webfolio/和WordPress版本是3.2.1。

魔术字段插件http://magicfields.org/

+0

这是正确的,你是覆盖你的图像变量内foreach循环?如果是这样,将is_array($ images)更改为!empty($ images) – Oliver 2013-03-04 03:45:38

+0

错误仍然相同..我的问题与此人相同https://groups.google.com/forum/?fromgroups=#!topic/magic-字段/ Sw4KxMAvQ_A仍然没有解决方案.. – rusly 2013-03-04 03:51:40

回答

0

此问题是因为,在array_reverse()sort()要传递的变量而言非阵列。

解决方案:

  1. 检查参数,执行这些功能之前,执行这些 功能只有在参数数组。您可以使用is_array 函数。

    if(is_array($array)){ 
        sort($array); 
    } 
    
  2. 检查你的参数是否为null或非数组使其成为数组。然后将其传递到函数。

    if(!is_array($array) || $array = "" || $array = NULL){ 
        $array = array(); 
    } 
    sort($array); 
    

我建议你的第二个解决方案,因为即使数组为null也不会影响到其他功能。

+0

我会尽力现在.. – rusly 2013-03-04 04:53:03