2

我想用 PHP 将对附加到数组中,这就是我目前拥有的;

pairs[0]['key1'] = 'something1';
pairs[0]['key2'] = 'something2';
pairs[1]['key1'] = 'other1';
pairs[1]['key2'] = 'other2';
pairs[2]['key1'] = 'thing1';
pairs[2]['key2'] = 'thing2';

有没有办法不必指定索引?即伪代码;

pairs[] = [key1=something1,key2=something2]
4

3 回答 3

2
for ($i=0;$i<=10;$i++){
    $arr[] = array("key"=>$i,"info"=>"something" . $i);
}
print_r($arr);
// output => Array ( [0] => Array ( [key] => 0 [info] => something0 ) [1] => Array ( [key] => 1 [info] => something1 ) [2] => Array ( [key] => 2 [info] => something2 ) ) ... 
于 2013-02-25T07:24:32.693 回答
0

array_push()做你想要的

array_push($pairs, array('key1' => 'something1', 'key2' => 'something2'))
于 2013-02-25T07:24:03.500 回答
0

也许尝试:

$pairs[]=array(key1 => "something1", key2 => "something2")

或者

$pairs[]=array_combine(array("key1","key2"),array("something1","something2"));

(未测试)

于 2013-02-25T07:29:51.260 回答