看看这段代码:
$GET = array();
$key = 'one=1';
$rule = explode('=', $key);
/* array_push($GET, $rule[0] => $rule[1]); */
我正在寻找这样的东西,以便:
print_r($GET);
/* output: $GET[one => 1, two => 2, ...] */
有没有一个函数来做到这一点?(因为array_push
不能以这种方式工作)
没有array_push()
可以用于关联数组,因为没有办法确定下一个键。
你需要使用
$arrayname[indexname] = $value;
将值推入数组会自动为其创建一个数字键。
当向数组添加键值对时,你已经有了键,不需要为其创建一个。将键推入数组没有意义。你只能设置数组中特定键的值。
// no key
array_push($array, $value);
// same as:
$array[] = $value;
// key already known
$array[$key] = $value;
您可以使用联合运算符 (+
) 来组合数组并保留添加的数组的键。例如:
<?php
$arr1 = array('foo' => 'bar');
$arr2 = array('baz' => 'bof');
$arr3 = $arr1 + $arr2;
print_r($arr3);
// prints:
// array(
// 'foo' => 'bar',
// 'baz' => 'bof',
// );
所以您可以执行$_GET += array('one' => 1);
。array_merge
的使用更多信息,请参阅http://php.net/manual/en/function.array-merge.php中的文档。array_merge()
和+
运算符之间的基本区别在于,当两个数组包含相同键时,+
运算符会忽略第二个数组中的值(不进行覆盖),而且它不会重新对数字键进行编号/重置。 - jave.web我不知道为什么最简单的方法还没有被发布:
$arr = ['company' => 'Apple', 'product' => 'iPhone'];
$arr += ['version' => 8];
array_merge
和数组联合(+=
)的行为方式相反,即array_merge将从第二个数组中保留值,并且数组联合将从第一个数组中保留值。 - santiago arizti$arr += [$key => $value];
就可以完美地实现它了,谢谢! - user7622147我想向表格中添加我的答案,这是它:
//connect to db ...etc
$result_product = /*your mysql query here*/
$array_product = array();
$i = 0;
foreach ($result_product as $row_product)
{
$array_product [$i]["id"]= $row_product->id;
$array_product [$i]["name"]= $row_product->name;
$i++;
}
//you can encode the array to json if you want to send it to an ajax call
$json_product = json_encode($array_product);
echo($json_product);
希望这可以帮助到某些人
正如Pekka所说的那样…
或者,如果您愿意,您可以像这样使用array_merge:
array_merge($_GET, array($rule[0] => $rule[1]));
但我更喜欢Pekka的方法,因为它更简单。
很多答案,有些有帮助,其他的好但笨拙。由于在像将元素添加到数组这样的简单操作中不需要复杂和昂贵的算术运算、循环等,这里是我收集的一行代码添加到数组函数。
$array = ['a' => 123, 'b' => 456]; // init Array
$array['c'] = 789; // 1.
$array += ['d' => '012']; // 2.
$array = array_merge($array, ['e' => 345]); // 3.
$array = [...$array, 'f' => 678]; // 4.
print_r($array);
// Output:
/*
Array
(
[a] => 123
[b] => 456
[c] => 789
[d] => 012
[e] => 345
[f] => 678
)
*/
99.99% 的时间我使用版本1。($array['c'] = 789;
)。但我喜欢版本4。那是带有splat运算符的版本(https://www.php.net/manual/zh/migration56.new-features.php)。
$main_array = array(); //Your array that you want to push the value into
$value = 10; //The value you want to push into $main_array
array_push($main_array, array('Key' => $value));
json_encode($main_array)
,那么它看起来像[{"Key":"10"}]
。我刚刚在寻找同样的东西,然后我意识到,一次又一次地,我的想法都不同,因为我是老派的。我追溯到BASIC和PERL时代,有时候会忘记PHP有多么简单。
我刚刚创建了这个函数,从数据库中获取所有设置,其中有3列。setkey,item(键)和value(值),并将它们放入一个名为settings的数组中,使用相同的键/值而不使用push,就像上面一样。
真的很容易和简单
// 获取所有设置 $settings=getGlobalSettings();
// 应用用户主题选择 $theme_choice = $settings['theme'];
.. etc etc etc ....
function getGlobalSettings(){
$dbc = mysqli_connect(wds_db_host, wds_db_user, wds_db_pass) or die("MySQL Error: " . mysqli_error()); mysqli_select_db($dbc, wds_db_name) or die("MySQL Error: " . mysqli_error()); $MySQL = "SELECT * FROM systemSettings"; $result = mysqli_query($dbc, $MySQL); while($row = mysqli_fetch_array($result)) { $settings[$row['item']] = $row['value']; // 不需要PUSH } mysqli_close($dbc); return $settings; }
所以,就像其他帖子解释的那样......在php中,当您使用
Key => Value
时,没有必要“PUSH”数组
而且......也不需要先定义数组。
$array=array();
不需要定义或推送。只需分配$array[$key] = $value;它自动是一个推送和声明。
我必须补充说,出于安全原因,(P)oor(H)elpless(P)rotection,我的意思是为了《PHP编程指南》的初学者们,我建议您仅将此概念用于我打算使用的目的。任何其他方法都可能存在安全风险。好了,我做出了免责声明!
Class Form {
# Declare the input as property
private $Input = [];
# Then push the array to it
public function addTextField($class,$id){
$this->Input ['type'][] = 'text';
$this->Input ['class'][] = $class;
$this->Input ['id'][] = $id;
}
}
$form = new Form();
$form->addTextField('myclass1','myid1');
$form->addTextField('myclass2','myid2');
$form->addTextField('myclass3','myid3');
array (size=3)
'type' =>
array (size=3)
0 => string 'text' (length=4)
1 => string 'text' (length=4)
2 => string 'text' (length=4)
'class' =>
array (size=3)
0 => string 'myclass1' (length=8)
1 => string 'myclass2' (length=8)
2 => string 'myclass3' (length=8)
'id' =>
array (size=3)
0 => string 'myid1' (length=5)
1 => string 'myid2' (length=5)
2 => string 'myid3' (length=5)
$arrayname = array('indexname1' => $value1, 'indexname2' => $value2);
会将它们设置为$arrayname
中唯一的项。如果您已经有了$arrayname
并希望保留其值,则可以尝试$arrayname += $anotherarray
。请注意,第一个数组中的任何现有键都将被第二个数组覆盖。 - Charlie Schliesser$a = array("name" => "John"); $a += array("name" => "Tom");
,那么$a ["name"]
将是"John"。 - santiago arizti