如何在 PHP 中向空数组添加元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/676677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add elements to an empty array in PHP?
提问by AquinasTub
If I define an array in PHP such as (I don't define its size):
如果我在 PHP 中定义一个数组,例如(我没有定义它的大小):
$cart = array();
Do I simply add elements to it using the following?
我是否只需使用以下内容向其中添加元素?
$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;
Don't arrays in PHP have an add method, for example, cart.add(13)?
PHP 中的数组没有 add 方法,例如,cart.add(13)?
回答by Bart S.
Both array_pushand the method you described will work.
无论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>";
Is the same as:
是相同的:
<?php
$cart = array();
array_push($cart, 13);
array_push($cart, 14);
// Or
$cart = array();
array_push($cart, 13, 14);
?>
回答by OIS
It's better to not use array_pushand just use what you suggested. The functions just add overhead.
最好不要使用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';
回答by andi
You can use array_push. It adds the elements to the end of the array, like in a stack.
您可以使用array_push。它将元素添加到数组的末尾,就像在堆栈中一样。
You could have also done it like this:
你也可以这样做:
$cart = array(13, "foo", $obj);
回答by fico7489
Based on my experience, you solution is fine(best) when keys are not important:
根据我的经验,当密钥不重要时,您的解决方案很好(最好):
$cart = [];
$cart[] = 13;
$cart[] = "foo";
$cart[] = obj;
回答by T.Todua
REMEMBER, this method overwrites first array, so use only when you are sure!
请记住,此方法会覆盖第一个数组,因此请仅在您确定时使用!
$arr1 = $arr1 + $arr2;
(见来源)
回答by unpluggeDloop
$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>";
回答by Isuru Eshan
$products_arr["passenger_details"]=array();
array_push($products_arr["passenger_details"],array("Name"=>"Isuru Eshan","E-Mail"=>"[email protected]"));
echo "<pre>";
echo json_encode($products_arr,JSON_PRETTY_PRINT);
echo "</pre>";
//OR
$countries = array();
$countries["DK"] = array("code"=>"DK","name"=>"Denmark","d_code"=>"+45");
$countries["DJ"] = array("code"=>"DJ","name"=>"Djibouti","d_code"=>"+253");
$countries["DM"] = array("code"=>"DM","name"=>"Dominica","d_code"=>"+1");
foreach ($countries as $country){
echo "<pre>";
echo print_r($country);
echo "</pre>";
}
回答by Gestix Team
When one wants elements to be added with zero-based element indexing, I guess this will work as well:
当一个人想要使用从零开始的元素索引添加元素时,我想这也会起作用:
// adding elements to an array with zero-based index
$matrix= array();
$matrix[count($matrix)]= 'element 1';
$matrix[count($matrix)]= 'element 2';
...
$matrix[count($matrix)]= 'element N';

