LoginSignup
0
1

More than 5 years have passed since last update.

配列が空かどうかを判定する

Posted at

入れ子で配列が返ってくる場合に中身が空かどうかを判定する関数。
ACFを利用する場合は、have_rows関数でも問題ないと思う。

ソースコード

functions.php
<?php
/**
* 配列の空判定
**/
function check_ary($array){

    if(is_array($array) && !empty($array)){

        foreach($array as $item){
            if(is_array($item)){
                if(check_ary($item)){
                    return true;
                }
            }else{
                if(!empty($item)){
                    return true;
                }
            }
        }
    }else{

        if(!empty($array)){
            return true;
        }
    }

    return false;
}
?>

使用例

ソース
<?php
$fields = get_field($field_name);
if(check_ary($fields)):
?>
<p>値が存在する場合の処理を記述する</p>
<?php endif; ?>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1