简体   繁体   English

在数组PHP中插入值

[英]insert value in array PHP

im new in PHP. 我是PHP新手。 just a simple question : 只是一个简单的问题:

Coding : 编码:

foreach($group as $b)
{
  if($b == 0){
       echo "error";
  }
  else{
        echo "true";
  }
}

i want value $b that "true" add to new array. 我希望值$ b“true”添加到新数组。

thanks. 谢谢。

$arr = array();
foreach($group as $b) {
    if ($b == 0) {
        echo "error";
    } else {
        echo "true";
        $arr[] = $b;
    }
}

只需使用array_push()

array_push($array, "true");
  1. Define the array. 定义数组。

  2. Push the data into 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?
    }
}

use array_push check 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

Use it: 用它:

array_push($arr,"true");

or 要么

echo "true";
$arr[] = $b;

To know more about array_push read this : 要了解有关array_push的更多信息,请阅读:

http://php.net/manual/en/function.array-push.php http://php.net/manual/en/function.array-push.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM