向数组添加键

时间:2015-06-23 05:32:16

标签: php arrays key

我读取文件(answers.txt)并将值写入数组

$filename = 'answers.txt';
$file = file($filename);

如何向数组添加密钥? answers.txt看起来像:

212
150
200
212

我需要获得以下密钥:

$array =  array (
                  "first" => "212",
                  "second" => "150",
                  "third" => "200",
                  "four" => "212"
                );

1 个答案:

答案 0 :(得分:1)

希望你正在寻找这个

 $keys=array("first","second","third","four");

 $values=array(212,150,200,212);

 $array_combine=array_combine($keys,$values);

 print_r($array_combine);