看看这段代码:
$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不能这样工作)
当前回答
这里已经给出了一些很好的例子。只是添加了一个简单的例子,将关联数组元素推到根数值索引索引。
$intial_content = array();
if (true) {
$intial_content[] = array('name' => 'xyz', 'content' => 'other content');
}
其他回答
我想把我的答案补充到表格中,如下:
//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);
希望这能帮助到一些人
不,关联数组没有等价的array_push(),因为没有办法确定下一个键。
你必须使用
$arrayname[indexname] = $value;
$arr = array("key1"=>"value1", "key2"=>"value");
print_r($arr);
/指纹阵列望远镜(key1”= >“value1”、“key2 = >“value2]
佩卡就是这么说的…
或者,你也可以像这样使用array_merge:
array_merge($_GET, array($rule[0] => $rule[1]));
但我更喜欢Pekka的方法,因为它更简单。
这里已经给出了一些很好的例子。只是添加了一个简单的例子,将关联数组元素推到根数值索引索引。
$intial_content = array();
if (true) {
$intial_content[] = array('name' => 'xyz', 'content' => 'other content');
}