开发者

check all values in table or object

开发者 https://www.devze.com 2023-04-07 03:37 出处:网络
i would like make somethings: <?php $a = array(); for ($i =0; $i<15; $i++){ $a[$i] = \'111\'; } foreach ($a as $ok){

i would like make somethings:

<?php 
$a = array();
for ($i =0; $i<15; $i++){
 $a[$i] = '111';
}

foreach ($a as $ok){
 //if all values in $a == 开发者_运维问答111 : {
echo "all is 111"
} else {
echo "no";
}
}

?>

LIVE: http://codepad.org/RdvhK0VD

is function in PHP for this? i must each values check separately?


You can use array_count_values which will return an associative array of values and their frequencies.

If array_count_values($a) returns an array of length 1 and its key is '111', then $a contains only '111'.

$arr2 = array_count_values($a);
$key = '111';

if( count($arr2) == 1 && array_key_exists($key, $a) )
{
  // $a contains only $key
}


<?php 
    $a = array();
    for ($i =0; $i<15; $i++)
    {
        $a[$i] = '111';
    }
    $flag=true;
    foreach ($a as $ok)
    {
        if ( $a != 111 )
        {
            $flat=true;
        }
    }
    if(flag===true)
    {
        echo "all is 111"
    } else {
        echo "no";
    }
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号