2013-03-04 32 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