看看这段代码:
$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不能这样工作)
看看这段代码:
$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_merge:
array_merge($_GET, array($rule[0] => $rule[1]));
但我更喜欢Pekka的方法,因为它更简单。
我只是在寻找同样的东西,然后我又一次意识到,我的想法是不同的,因为我是守旧派。我一直在用BASIC和PERL,有时我忘记了PHP有多简单。
我刚刚做了这个函数,以采取所有设置从数据库,其中他们是3列。Setkey, item (key) & value (value),并将它们放入一个名为Settings的数组中,使用相同的键/值,而不像上面那样使用push。
真的很简单
// Get All Settings $settings=getGlobalSettings(); // Apply User Theme Choice $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']; // NO NEED FOR PUSH } mysqli_close($dbc); return $settings; }
所以就像其他帖子解释的那样……在php中,当你使用数组时,不需要“PUSH”数组
Key =>值
和…也不需要先定义数组。
$array=数组();
不需要定义或强迫。赋值$array[$key] = $value;它自动同时是一个推送和一个声明。
我必须补充一点,出于安全原因,(P)oor (H)elpless (P)保护,我的意思是为傻瓜编程,我的意思是PHP....呵呵,我建议你只用这个概念来表达我的意图。任何其他方法都可能存在安全风险。在那里,我做了免责声明!
将值压入数组会自动为其创建一个数字键。
当向数组添加键-值对时,您已经拥有键,不需要为您创建一个键。将键推入数组是没有意义的。只能设置数组中特定键的值。
// 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的文档。
我想把我的答案补充到表格中,如下:
//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);
希望这能帮助到一些人
有点晚了,但如果你不介意嵌套数组,你可以采用这种方法:
$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"}]
这是可能对你有用的解
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)
有点奇怪,但这对我很管用
$array1 = array("Post Slider", "Post Slider Wide", "Post Slider");
$array2 = array("Tools Sliders", "Tools Sliders", "modules-test");
$array3 = array();
$count = count($array1);
for($x = 0; $x < $count; $x++){
$array3[$array1[$x].$x] = $array2[$x];
}
foreach($array3 as $key => $value){
$output_key = substr($key, 0, -1);
$output_value = $value;
echo $output_key.": ".$output_value."<br>";
}
$arr = array("key1"=>"value1", "key2"=>"value");
print_r($arr);
/指纹阵列望远镜(key1”= >“value1”、“key2 = >“value2]
我想知道为什么最简单的方法还没有公布:
$arr = ['company' => 'Apple', 'product' => 'iPhone'];
$arr += ['version' => 8];
嗨,我也有同样的问题,我找到了这个解决方案,你应该使用两个数组,然后将它们结合起来
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
参考:w3schools
简单的方法:
$GET = array();
$key = 'one=1';
parse_str($key, $GET);
http://php.net/manual/de/function.parse-str.php
例子array_merge()……
$array1 = array("color" => "red", 2,4); $ array2 =阵列(“a”、“b”、“颜色”= >“绿色”、“形状”= >“梯形”,4); $result = array_merge($array1, $array2); print_r(结果);
阵列([color] = >绿色,[0]= > 2,[1]= > 4,[2]= >,[3]= > b(形状)= >梯形,[4]= > 4,)
用于添加到第一个位置的键和值
$newAarray = [newIndexname => newIndexValue] ;
$yourArray = $newAarray + $yourArray ;
我写了一个简单的函数:
function push(&$arr,$new) {
$arr = array_merge($arr,$new);
}
这样我就可以很容易地“upsert”新元素:
push($my_array, ['a'=>1,'b'=>2])
这里已经给出了一些很好的例子。只是添加了一个简单的例子,将关联数组元素推到根数值索引索引。
$intial_content = array();
if (true) {
$intial_content[] = array('name' => 'xyz', 'content' => 'other content');
}
我通常这样做:
$array_name = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
2023
有很多答案。有些有用,有些不错,但有些尴尬。因为你不需要复杂而昂贵的算术运算,循环等简单的操作,如添加一个元素到数组,这里是我的一行添加到数组的函数集合。
$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%的情况下,我使用版本1。($array['c'] = 789;)但我喜欢版本4。这是带有splat操作符的版本(https://www.php.net/manual/en/migration56.new-features.php)。