看看这段代码:

$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_name = array(
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3'
);

其他回答

 $arr = array("key1"=>"value1", "key2"=>"value");
    print_r($arr);

/指纹阵列望远镜(key1”= >“value1”、“key2 = >“value2]

例子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,)

有点奇怪,但这对我很管用

    $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>";
    }

这是可能对你有用的解

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)

不,关联数组没有等价的array_push(),因为没有办法确定下一个键。

你必须使用

$arrayname[indexname] = $value;