php数组练习

php数组练习

定义数组

利用四种不同的方式定义四个角色的数组信息,包含姓名,性别,年龄,成绩

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $name   = "gjl";
    $sex    = true;
    $age    = 19;
    $score  = 99.9;
    
    $stu1[] = $name;
    $stu1[] = $sex;
    $stu1[] = $age;
    $stu1[] = $score;

    $name   = "psl";
    $sex    = true;
    $age    = 18;
    $score  = 89.9;

    $stu2['name']   =$name;
    $stu2['sex']    =$sex;
    $stu2['age']    =$age;
    $stu2['score']  =$score;

    $stu3['name']   ='zz';
    $stu3['sex']    = false;
    $stu3['age']    = 20;
    $stu3['score']  = 59.9;

    $stu4=array(
        'name'  =>'order',
        'sex'   => true,
        'age'   => 20,
        'score' => 85,
    );
    
    ?>
</body>
</html>

单个输出举例展示

image-20231023110717731

遍历输出数组信息

image-20231023113124452

用foreach实现遍历

foreach 语法结构提供了遍历数组的简单方式。

foreach 这种语言结构是专门用来遍历数组的,foreach 是一种循环,不需要额外计时器

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    $name   = "gjl";
    $sex    = true;
    $age    = 19;
    $score  = 99.9;
    
    $stu1[] = $name;
    $stu1[] = $sex;
    $stu1[] = $age;
    $stu1[] = $score;

    $name   = "psl";
    $sex    = true;
    $age    = 18;
    $score  = 89.9;

    $stu2['name']   =$name;
    $stu2['sex']    =$sex;
    $stu2['age']    =$age;
    $stu2['score']  =$score;

    $stu3['name']   ='zz';
    $stu3['sex']    = false;
    $stu3['age']    = 20;
    $stu3['score']  = 59.9;

    $stu4=array(
        'name'  =>'order',
        'sex'   => true,
        'age'   => 20,
        'score' => 85,
    );
    $stu5=array($stu1,$stu2,$stu3,$stu4);
    echo "<table border=2>";
    echo "<tr align='center'><td colspan=4>学生信息</td></tr>";
    echo "<tr><td>姓名</td><td>性别</td><td>年龄</td><td>成绩</td></tr>";
    foreach($stu5 as $k1 => $v1){
        echo "<tr>";
        foreach($v1 as $k => $v){
            echo "<td>".$v."</td>";
        }
    }
    echo "</table>";
    ?>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值