在数组 PHP 中插入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11425739/
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
insert value in array PHP
提问by Qusyaire Ezwan
im new in PHP. just a simple question :
我是 PHP 新手。只是一个简单的问题:
Coding :
编码:
foreach($group as $b)
{
if($b == 0){
echo "error";
}
else{
echo "true";
}
}
i want value $b that "true" add to new array.
我想要将“true”添加到新数组的值 $b。
thanks.
谢谢。
回答by alfasin
$arr = array();
foreach($group as $b) {
if ($b == 0) {
echo "error";
} else {
echo "true";
$arr[] = $b;
}
}
回答by Hassan
回答by Nadav S.
Define the array.
Push the datainto the array.
定义数组。
将数据推入数组。
Example:
例子:
$array = new array();
foreach ($group as $b) {
if ($b == 0) {
echo "error";
} else {
echo "true";
array_push($array,$b) //or any value?
}
}
回答by J.K.A.
Use it:
用它:
array_push($arr,"true");
or
或者
echo "true";
$arr[] = $b;
To know more about array_push read this :
要了解有关 array_push 的更多信息,请阅读:
回答by J.K.A.
use array_pushcheck this link
使用array_push检查此链接
$a = new array();
array_push($a,"true");
print_r($a);
We can add to a numerical array in these ways:
我们可以通过以下方式添加到数值数组中:
$arr = new array("true"); //Create the array & add the values
var_dump($arr); //Print the contents of the array to screen
You can also push values to an array:
您还可以将值推送到数组:
$arr = new array(); //Create the array
array_push($arr, 'true'); //'Push' the value into the next available index
var_dump($arr); //Print the contents of the array to screen
You can also add to array by directly setting the index:
您还可以通过直接设置索引来添加到数组:
$arr = new array(); //Create the array
$arr[0] = 'true'; //'Set' index 0 to the value
var_dump($arr); //Print the contents of the array to screen

