看一下这个代码。
$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
不会以这种方式工作)
你可以使用联合运算符(+
)来组合数组,并保留所添加数组的键。
例如:
<?php
$arr1 = array('foo' => 'bar');
$arr2 = array('baz' => 'bof');
$arr3 = $arr1 + $arr2;
print_r($arr3);
// prints:
// array(
// 'foo' => 'bar',
// 'baz' => 'bof',
// );
}
<?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);
希望对大家有所帮助
这就是可能对u有用的解决方案。
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)
我也在寻找同样的东西,我又一次意识到,我的思维是不同的,因为我是老派。我一直追溯到BASIC和PERL,有时我忘了在PHP中事情真的很简单。
我只是做了这个函数,从数据库中获取所有的设置,其中有3个列。 Setkey, item (key) & value (value),并将它们放入一个叫做settings的数组中,使用相同的key/value,而不像上面那样使用push。
很容易,也很简单,真的
// 获取所有设置 $settings=getGlobalSettings()。
//应用用户的主题选择 $theme_choice = $settings['theme']。
...等等等等 ....
函数 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);
返回 $settings。 }
所以就像其他帖子解释的那样...在php中,当你使用 "PUSH "数组时,没有必要对其进行"PUSH"。
Key => Value
而且...也不需要先定义数组。
$array=array()。
不需要定义也不需要推送。只需分配$array[$key] = $value; 它同时自动成为一个推送和声明。
我必须补充一点,出于安全考虑,(P)or (H)elpless (P)rotection,我是指Programming for Dummies,我是指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"}]。
有点奇怪,但这个对我很有效
$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>";
}
嗨,我有同样的问题,我发现这个解决方案,你应该使用两个数组,然后将它们两者结合起来
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
参考文献 : [w3schools][1]
例子 array_merge()...。
$array1 = array("color" => "red", 2, 4); $array2 = array("a", "b", "color" => "绿色","形状" =>,"梯形"。 "梯形", 4); $result = array_merge($array1, $array2); print_r($result);
。
Array([color] => 绿色,[0] => 2,[1] => 4,[2] => a,[3] => b,[形状]=> 梯形,[4]=>。 4,)