如何检查php中只包含int值的多维数组



如何检查php 中只包含int值的多维数组

有什么最容易检查的吗。

就像我有一个包含int值的数组,但我必须检查所有值是否在中

阵列1

$RoomGuests = array(
                        array(
                         "NoOfAdults"=> 2,
                         "NoOfChild"=>0,
                        ),
                        array(
                         "NoOfAdults"=> 3,
                         "NoOfChild"=>2,
                         "ChildAge"=> array(12,11)
                        ),
                        array(
                         "NoOfAdults"=> 3,
                         "NoOfChild"=>1,
                         "ChildAge"=> array(11)
                        )
                    );

以上数组仅包含int值,则为true

阵列2

$RoomGuests2 = array(
                        array(
                         "NoOfAdults"=> "two",
                         "NoOfChild"=>0,
                        ),
                        array(
                         "NoOfAdults"=> "three",
                         "NoOfChild"=>"two",
                         "ChildAge"=> array(12,11)
                        ),
                        array(
                         "NoOfAdults"=> 3,
                         "NoOfChild"=>1,
                         "ChildAge"=> array(11)
                        )
                    );

以上数组不包含int值,则为false

阵列3

$RoomGuests3 = array(
                    array(
                     "NoOfAdults"=> 2,
                     "NoOfChild"=>0,
                    ),
                    array(
                     "NoOfAdults"=> 3,
                     "NoOfChild"=>2,
                     "ChildAge"=> array(12,11)
                    ),
                    array(
                     "NoOfAdults"=> 3,
                     "NoOfChild"=>1,
                     "ChildAge"=> array(
                         "NoOfAdults"=> 3,
                         "NoOfChild"=>1,
                         "ChildAge"=> array(11,'k')
                        )
                    )
                );

它可能会帮助您

var_dump(is_IntArray($RoomGuests));
var_dump(is_IntArray($RoomGuests2));
var_dump(is_IntArray($RoomGuests3));
function is_IntArray($p){
    $Result = is_array($p) ?
            implode('', array_map(__FUNCTION__, array_fill(0, count($p), ''), $p)) : 
            $p;
    return ctype_digit($Result);
}

您可以使用foreach()循环父数组,并使用array_walk_recursive()函数检查子数组。

像这样:

$RoomGuests2 = array(
      array(
       "NoOfAdults"=> "two",
       "NoOfChild"=>0,
      ),
      array(
       "NoOfAdults"=> "three",
       "NoOfChild"=>"two",
       "ChildAge"=> array(12,11)
      ),
      array(
       "NoOfAdults"=> 3,
       "NoOfChild"=>1,
       "ChildAge"=> array(11)
      )
);
foreach($RoomGuest2 as $child) {
      array_walk_recursive($child, 'checkIfInt');
}
function checkIfInt($array, $key) {
      // If the child array is another array (like "ChildAge"=> array(11)) then check this array.
      if(is_array($array[$key])) array_walk_recursive($array[$key], 'checkIfInt');
      else if(!is_integer($array[$key])) {
            // If the value it's not a integer, then PHP will execute this code. 
      }
}

为什么不只是:

function containsOnlyIntegers($array) {
    foreach($array as $arr) {
        foreach($arr as $k => $a) {
            if ($k === 'NoOfAdults' || $k === 'NoOfChild') {
                if (!is_int($a)) { return false; }
            }
        }
    }
    return true;
}

相关内容

  • 没有找到相关文章

最新更新