Aqu*_*Tub 458 php arrays variables
如果我在PHP中定义一个数组,例如(我没有定义它的大小):
$cart = array();
Run Code Online (Sandbox Code Playgroud)
我只是使用以下内容添加元素吗?
$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;
Run Code Online (Sandbox Code Playgroud)
PHP中的数组是否有添加方法,例如,cart.add(13)?
Bar*_* S. 739
两者array_push和你描述的方法都有效.
$cart = array();
$cart[] = 13;
$cart[] = 14;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i=0;$i<=5;$i++){
$cart[] = $i;
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
是相同的:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
Run Code Online (Sandbox Code Playgroud)
OIS*_*OIS 70
最好不要使用array_push,只使用你的建议.这些功能只会增加开销.
//We don't need to define the array, but in many cases it's the best solution.
$cart = array();
//Automatic new integer key higher than the highest
//existing integer key in the array, starts at 0.
$cart[] = 13;
$cart[] = 'text';
//Numeric key
$cart[4] = $object;
//Text key (assoc)
$cart['key'] = 'test';
Run Code Online (Sandbox Code Playgroud)
fic*_*489 11
根据我的经验,当密钥不重要时,解决方案很好(最好):
$cart = [];
$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;
Run Code Online (Sandbox Code Playgroud)
and*_*ndi 10
你可以使用array_push.它将元素添加到数组的末尾,就像在堆栈中一样.
你也可以这样做:
$cart = array(13, "foo", $obj);
Run Code Online (Sandbox Code Playgroud)
小智 5
$cart = array();
$cart[] = 11;
$cart[] = 15;
// etc
//Above is correct. but below one is for further understanding
$cart = array();
for($i = 0; $i <= 5; $i++){
$cart[] = $i;
//if you write $cart = [$i]; you will only take last $i value as first element in array.
}
echo "<pre>";
print_r($cart);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
926718 次 |
| 最近记录: |