定义多维数组,存放本宿舍同学的姓名,电话,QQ等,并遍历该多维数组,一行显示一位同学。
时间: 2024-06-09 21:09:21 浏览: 119
可以使用二维数组来存放宿舍同学的姓名,电话和 QQ 号码等信息,代码如下:
```python
dormitory = [
["张三", "13312345678", "123456789"],
["李四", "13387654321", "987654321"],
["王五", "13987654321", "456789123"],
["赵六", "13912345678", "321654987"]
]
# 遍历多维数组,一行显示一位同学
for student in dormitory:
print("姓名:%s,电话:%s,QQ:%s" % (student[0], student[1], student[2]))
```
输出结果如下:
```
姓名:张三,电话:13312345678,QQ:123456789
姓名:李四,电话:13387654321,QQ:987654321
姓名:王五,电话:13987654321,QQ:456789123
姓名:赵六,电话:13912345678,QQ:321654987
```
相关问题
php定义多维数组,存放本宿舍同学的姓名,电话,QQ等,并遍历该多维数组,一行显示一位同学。
可以使用以下方式定义一个本宿舍同学的多维数组:
```php
$students = array(
array("name" => "张三", "phone" => "123456789", "qq" => "123456"),
array("name" => "李四", "phone" => "987654321", "qq" => "654321"),
array("name" => "王五", "phone" => "135792468", "qq" => "789456")
);
```
然后可以使用foreach循环来遍历该多维数组,并一行显示一位同学的信息:
```php
foreach ($students as $student) {
echo "姓名:" . $student['name'] . ",电话:" . $student['phone'] . ",QQ:" . $student['qq'] . "<br>";
}
```
输出结果为:
```
姓名:张三,电话:123456789,QQ:123456
姓名:李四,电话:987654321,QQ:654321
姓名:王五,电话:135792468,QQ:789456
```
阅读全文