php - PHP 数组插入语句记录在哪里?
问题描述
在 PHP 中,以下语句既定义了一个数组,又将一个元素插入到数组中:
$arr[] = $element
下面显示了使用此语句的示例(在循环中)。(它来自这个PHP MySQli 教程):
while ($row = $result->fetch_assoc())
{
$ids[] = $row['id'];
$names[] = $row['name'];
$ages[] = $row['age'];
}
这个语法在官方 PHP 文档中记录在哪里?
请解释这个语法。
解决方案
循环中的array_push(您正在使用while循环)和初始化数组($arr[] = $element
)的值并没有什么特别之处。唯一的区别是您通过 while 循环将值推入数组并最初使用赋值运算符声明。
在您的情况下,您已将 $element 初始化为数组
$arr[] = "123"; //Lets say 123 is $element
$arr[] = "NAME"; //Same way, you can repeat in next line as well
$arr[] = "45";
echo '<pre>';
print_r($arr);
// And the output was
Array (
[0] => 123
[1] => NAME
[2] => 45
)
下一个声明是
while ($row = $result->fetch_assoc())
{
$ids[] = $row['id'];// Lets say 123 is $row['id'];
$names[] = $row['name']; //Lets say NAME is $row['name'];
$ages[] = $row['age']; //Lets say 45 is $row['age'];
}
//The output will be same if only one row exist in the loop
Array (
[0] => 123
[1] => NAME
[2] => 45
.................. // Based on while loop rows
)
符号[]会将值推入数组,因为您没有在括号之间提及任何键。
推荐阅读
- excel - 如何将具有特定标签颜色的工作表从一个工作簿复制到另一个工作簿?
- junit5 - 在 Junit 5 Jupiter 版本 5.4.0-RC1 中找不到 ClassExtensionContext
- media - 准备发送媒体
- azure - 没有 ConnectionString 的 ASP.NET Core 发布
- javascript - 尽管我们在 html 表中有数据,但仍显示“表中没有可用数据”
- excel - 将值的最后一行范围与 VBA 中的一组行进行比较
- angular - 如何在 loadChildren 中为 Nativescript 和 Angular 使用相同的路径?
- bash - 根据列表对路径更改的所有文件执行 mv
- sql-server - 将 MDX 查询转换为 SQL 查询
- c# - Xamarin 初始化顺序