php - Read data from a file into an associative array -


i have file read.txt records this

pulp_fiction pulp fiction jurassic_park jurassic park inception inception 

i want read these content of file associative array in quest.php

<?php  $quest ["pulp_fiction"] = "pulp fiction";  $quest ["jurassic_park"] = "jurassic park";  $quest ["inception"] = "inception"; 

this code opens file writing in quest.php need in array part. thks

<?php $myfile = "read.txt"; $fh = fopen($myfile, 'r'); $thedata = fread($fh, filesize($myfile)); $assoc_array = array(); $my_array = explode("\n", $thedata); foreach($my_array $line) {     $tmp = explode("\n", $line);     $assoc_array[$tmp[0]] = $tmp[1]; } fclose($fh);  // op wants results in $quest $quest = $assoc_array; ?> 

i have section of code saved quest.php , calling in quiz.php when try match image title actual title, nothing populates.

probably slicker way, first thought:

$lines = file("read.txt", file_ignore_new_lines); $pairs = array_chunk($lines, 2);  foreach($pairs $array) {     $quest[$array[0]] = $array[1]; } 

needs var , error checking.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -