看看这段代码:

$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()……

$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,)

其他回答

嗨,我也有同样的问题,我找到了这个解决方案,你应该使用两个数组,然后将它们结合起来

 <?php

$fname=array("Peter","Ben","Joe");

$age=array("35","37","43");

$c=array_combine($fname,$age);

print_r($c);

?>

参考:w3schools

我想把我的答案补充到表格中,如下:

//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);

希望这能帮助到一些人

我写了一个简单的函数:

function push(&$arr,$new) {
    $arr = array_merge($arr,$new);
}

这样我就可以很容易地“upsert”新元素:

push($my_array, ['a'=>1,'b'=>2])

我通常这样做:

$array_name = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);
array_push($GET, $GET['one']=1);

这对我很管用。