2015-06-23 93 views
0

我讀文件(answers.txt)和寫值到一個數組添加鍵陣列

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

我如何添加鍵給數組? answers.txt樣子:

212 
150 
200 
212 

我需要得到像鑰匙:

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

嘗試使用['array_combine'(HTTP:// PHP .net/manual/en/function.array-combine.php) –

+0

文件中的數值是無限的,還是隻有四個? – someOne

+3

重複這或許:http://stackoverflow.com/questions/6116056/php-increment-counter-function-using-words-i-e-first-second-third-etc – Rasclatt

回答

1

希望你正在尋找這個

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

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

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

print_r($array_combine);