php 将数据动态添加到数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3325009/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:20:28  来源:igfitidea点击:

Add data dynamically to an Array

php

提问by sikas

I want to add data to an array dynamically.

我想动态地将数据添加到数组中。

回答by TimCinel

There are quite a few ways to work with dynamic arrays in PHP. Initialise an array:

有很多方法可以在 PHP 中使用动态数组。初始化一个数组:

$array = array();

Add to an array:

添加到数组:

$array[] = "item";
$array[$key] = "item";
array_push($array, "item", "another item");

Remove from an array:

从数组中删除:

$item = array_pop($array);
$item = array_shift($array);
unset($array[$key]);

There are plenty more ways, these are just some examples.

还有很多方法,这些只是一些例子。

回答by NikiC

$array[] = 'Hi';

pushes on top of the array.

推到数组的顶部。

$array['Hi'] = 'FooBar';

sets a specific index.

设置特定索引。

回答by Himadri

Let's say you have defined an empty array:

假设您定义了一个空数组:

$myArr = array();

If you want to simply add an element, e.g. 'New Element to Array', write

如果你想简单地添加一个元素,例如'New Element to Array',写

$myArr[] = 'New Element to Array';

if you are calling the data from the database, below code will work fine

如果您从数据库中调用数据,下面的代码将正常工作

$sql = "SELECT $element FROM $table";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0)//if it finds any row
{
   while($result = mysql_fetch_object($query))
   {
      //adding data to the array
      $myArr[] = $result->$element;
   }
}

回答by Quy Le

You should use method array_push to add value or array to array exists

您应该使用方法 array_push 将值或数组添加到数组中

$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);

/** GENERATED OUTPUT
Array
(
    [0] => orange
    [1] => banana
    [2] => apple
    [3] => raspberry
)
*/

回答by Mewp

Like this?:

像这样?:

$array[] = 'newItem';

回答by Gordon

In additon to directly accessing the array, there is also

除了直接访问数组,还有

array_push— Push one or more elements onto the end of array

array_push— 将一个或多个元素压入数组末尾

回答by Dherya

$dynamicarray = array();

for($i=0;$i<10;$i++)
{
    $dynamicarray[$i]=$i;
}

回答by majick

just for fun...

只是为了好玩...

$array_a = array('0'=>'foo', '1'=>'bar');
$array_b = array('foo'=>'0', 'bar'=>'1');

$array_c = array_merge($array_a,$array_b);

$i = 0; $j = 0;
foreach ($array_c as $key => $value) {
    if (is_numeric($key)) {$array_d[$i] = $value; $i++;}
    if (is_numeric($value)) {$array_e[$j] = $key; $j++;}
}

print_r($array_d);
print_r($array_e);

回答by Joseph Brokefoot Williams

Fastest way I think

我认为最快的方式

       $newArray = array();

for($count == 0;$row = mysql_fetch_assoc($getResults);$count++)
    {
    foreach($row as $key => $value)
    { 
    $newArray[$count]{$key} = $row[$key];
    }
}

回答by ShivarajRH

Adding array elements dynamically to an Array And adding new element to an Array

将数组元素动态添加到数组并将新元素添加到数组

$samplearr=array();
$count = 0;
foreach ($rslt as $row) {
        $arr['feeds'][$count]['feed_id'] = $row->feed_id;
        $arr['feeds'][$count]['feed_title'] = $row->feed_title;
        $arr['feeds'][$count]['feed_url'] = $row->feed_url;
        $arr['feeds'][$count]['cat_name'] = $this->get_catlist_details($row->feed_id);
        foreach ($newelt as $cat) {
            array_push($samplearr, $cat);              
        }
        ++$count;
}
$arr['categories'] = array_unique($samplearr); //,SORT_STRING

$response = array("status"=>"success","response"=>"Categories exists","result"=>$arr);