[英]insert value in array PHP
我是PHP新手。 只是一个简单的问题:
编码:
foreach($group as $b)
{
if($b == 0){
echo "error";
}
else{
echo "true";
}
}
我希望值$ b“true”添加到新数组。
谢谢。
$arr = array();
foreach($group as $b) {
if ($b == 0) {
echo "error";
} else {
echo "true";
$arr[] = $b;
}
}
只需使用array_push()
。
array_push($array, "true");
定义数组。
将数据推入阵列。
例:
$array = new array();
foreach ($group as $b) {
if ($b == 0) {
echo "error";
} else {
echo "true";
array_push($array,$b) //or any value?
}
}
使用array_push检查此链接
$a = new array();
array_push($a,"true");
print_r($a);
我们可以通过以下方式添加数值数组:
$arr = new array("true"); //Create the array & add the values
var_dump($arr); //Print the contents of the array to screen
您还可以将值推送到数组:
$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
您还可以通过直接设置索引来添加到数组:
$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
用它:
array_push($arr,"true");
要么
echo "true";
$arr[] = $b;
要了解有关array_push的更多信息,请阅读:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.