PHP Is_array()函数

PHP Is_array()函数

数组 – 数组是一种特定的数据类型,允许我们在特定变量下存储多个相同类型的数据组件,这种方法可以帮助我们减少创建多个变量来存储每个数据所需的时间和能量。我们可以使用索引访问数组的每个元素,例如,如果我们需要存储5个不同员工的工资,而不是为每个员工创建5个变量,我们可以轻松地创建一个名为salary的数组,并将所有员工的工资放在一个变量salary中。在这种情况下,创建5个变量仍然是可行的任务,但是如果我们必须存储100个甚至1000个员工的工资,对于这种情况,数组是最好的选择。

Is_array() - 是PHP的另一个内置函数,用于判断一个定义的变量是数组还是其他类型的变量。

语法:

is_array ( $ variable name)

is_array函数接受一个参数,$变量名是已声明的要检查的变量的名称。is_array()函数返回一个布尔值,即如果变量是数组,则返回TRUE,否则返回FALSE。

示例

<!DOCTYPE html>
<html>
<body>

<?php
a = " good morning " ; //variable a with
print_r (a) ; 
echo " is " ;
if(is_array (a) = = 1){
echo " array " . "
" ; } else{ echo " not an array " . " <br> " ; }
b = array ( " cyan " , " magenta " , " yellow " ) ; //variable with an array print_r (b); // inbuilt PHP function to print array echo " is " ; if( is_array (b) = = 1){ echo " an array " . " < br > " ; } else{ echo " not an array " . " < br > " ; } c = array ( " doctor " => " 35LPA " , " engineer " => " 27LPA " , " accountant " => " 20LPA " ) ; print_r (c) ; echo " is " ; if(is_array (c) = = 1){ echo "an array " . "
" ; } else{ echo " not an array " . "
" ; }
d = " cyan , magenta , yellow " ; print_r(d); echo " is "; if(is_array(d) = = 1){ echo " array " . " < br > " ; } else{ echo " not an array " . " < br > " ; } ?> </body> </html>

输出:

good morning is not an array
Array ( [0] => cyan [1] => magenta [2] => yellow ) is an array
Array ( [doctor] => 35LPA [engineer] => 27LPA [accountant] => 20LPA ) is an array
cyan, magenta, yellow is not an array

在这个程序中,我们声明了多个具有不同数据类型的变量,并且通过if else语句来推断输出结果,判断声明的变量是否包含数组,我们使用is_array函数来检查声明的数据是否为数组。

示例:

<!DOCTYPE html>
<html>
<body>

<?php
a1 = 29.99;a2 = 42;
a3 = " abc ";a4 = array ( ' A ' ,  ' B ' , ' C ' ) ;

if (is_array (a4) ) {
    print_r(a4);
    echo " <br> ";
    foreach(a4 asvalue){
    echo value . " ";}
    echo " is an array " . " <br> "  . " <br> ";}
else
    echoa4 . " is not a array value" . " <br> " . " <br> ";


if (is_array(a1)){
    print_r(a1);
    echo "<br>";
    foreach(a1 asvalue){
    echo value . " ";}
    echo " is an array " . " <br> "  . " <br> ";}
else
    echo "a1 is not a array value. \n" . "<br>" . "<br>";

if (is_array(a2)){
    print_r(a2);
    echo "<br>";
    foreach(a2 asvalue){
    echo value . " ";}
    echo " is an array " . " <br> "  . " <br> "; }
else
    echo "a2 is not a array value. \n" . " <br> " . " <br> ";

if (is_array(a3)){
    print_r(a3);
    echo "<br>";
    foreach(a3 asvalue){
    echo value . " ";}
    echo " is an array " . " <br> "  . " <br> ";}
else
    echo "a3 is not a array value. \n" . " <br> ";
?>


</body>
</html>

输出:

Array ( [0] => A [1] => B [2] => C ) 
A B C is an array 

29.99 is not a array value. 

42 is not a array value. 

abc is not a array value.

在这个程序中,我们声明了多个具有不同数据的变量,并使用if else语句推断输出,以确定声明的变量是否包含数组,我们使用is_array函数检查声明的数据是否为数组。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程