1 php
这可能是基本问题,但如何在 PHP 中创建列表列表。我想执行以下操作:
var x = List(List(1,1,1),List(2,2,2), List(3,3))
echo x[0] //prints List(1,1,1)
echo x[1] //prints List(2,2,2)
x = x.List(4,4,4)
echo x //prints List(List(1,1,1),List(2,2,2), List(3,3),List(4,4,4))
Run Code Online (Sandbox Code Playgroud)
列表在 PHP 中被称为“数组”,它们很容易使用:
您可以通过指定其成员来实例化一个:
$x = array(
array(1, 1, 1),
array(2, 2, 2),
array(3, 3)
);
Run Code Online (Sandbox Code Playgroud)
要附加到数组,请使用空方括号:
$x[] = array(4, 4, 4);
Run Code Online (Sandbox Code Playgroud)
要访问特定成员,请将其键放在方括号中:
$x[0]; // array(1, 1, 1)
Run Code Online (Sandbox Code Playgroud)
这也适用于多维数组:
$x[0][2]; // 1
Run Code Online (Sandbox Code Playgroud)
要打印出一个数组以便您可以看到内容,请使用print_r或var_dump.. 虽然print_r对于数组来说更整洁。
echo $x; // "Array" .. not great
print_r($x);
/* something like this:
array(
0 => array (
0 => 1
1 => 1
2 => 1
)
1 => array (
.. etc
*/
Run Code Online (Sandbox Code Playgroud)
数组也可以使用字符串作为键:
$x['foo'] = 'bar';
Run Code Online (Sandbox Code Playgroud)
这些被称为“关联数组”。
| 归档时间: |
|
| 查看次数: |
3919 次 |
| 最近记录: |