|
|
||||||
|
#1
|
|
|
|
|
Hi,
I have a text file on a website. This file contains data like: 01,1;02,0;03,2;04,1 On a different webserver this file is read with php. I use the following: <?php $filename = "http://www.xyz.org/abcd.txt"; $handle = fopen ($filename, "r"); $contents = fread ($handle , 4096); fclose ($handle); // This should transform 01,1;02,0 into "01" => "1" , "02" => "0" $result1 = str_replace(",", "\" => \"", $contents); $result2 = str_replace(";", "\", \"", $result1); $result3 = "\"" . $result2 . "\""; // Sets the string as associative array $test = array($result3); // Display each key and value in a separate line foreach ($test as $key => $value) { echo "Key: $key; Value: $value<br />\n"; } ?> The result I get, is: Key: 0; Value: "01" => "1" , "02" => "0" , "03" => "2" , "04" => "1" If I echo $result3 I get the correct string, but why is it not valid for the assocciative array? Any help is very appreciated. Thanks, George |
|
|
|
#2
|
|
|
|
|
"George Malley" <george_malley> wrote in message
news:f777 [..] > } > > ?> > > The result I get, is: > > Key: 0; Value: "01" => "1" , "02" => "0" , "03" => "2" , "04" => "1" > > If I echo $result3 I get the correct string, but why is it not valid > for the assocciative array? I think that the row $test = array($result3); isn't correct because the argument is a string (so u obtain a Key 0 and the Value "the string") I think you have to change that row in: eval("\$test = array($result3);"); or similar. Bye |
|
#3
|
|
|
|
|
George Malley wrote:
[..] > The result I get, is: > > Key: 0; Value: "01" => "1" , "02" => "0" , "03" => "2" , "04" => "1" > > If I echo $result3 I get the correct string, but why is it not valid > for the assocciative array? > > Any help is very appreciated. > > Thanks, George Try: <?php $filename = "abcd.txt"; $handle = fopen ($filename, "r"); $contents = fread ($handle , 4096); fclose ($handle); $pie = explode(";", $contents); foreach ($pie as $v) { $slice = explode(",", $v); $arr[$slice[0]] = $slice[1]; } echo "<pre>";print_r($arr);echo "</pre>"; ?> |
|
|
| Similar Threads | |
| Help: Reading file and creating an associative array Semi-newbie here. Sorry for posting what is probably a simple question, but I'm stumped. I want to read in the records of a file and create an associative array with the key... |
|
| Regular Expression Result as Associative Array Index Hi Everyone. $layout=ereg_replace("\[\[([a-z:0-9]+)\]\]", $dillo['\\1'], $layout); I'm trying to use the result of a Regular Expression Match as an Index for the... |
|
| Unexpected values in an associative array Hi I think this is probably just a misunderstanding on my part, but I'm creating associative arrays with string keys from MySQL query results and when I put a value in the... |
|
| Unexpected result in array I am trying to list the directories, I have probably over complicated this so if there is a simple solution to my problem then that would be great too. directory... |
|
| [newbie]saving and reading array of associative array i'm looking for examples of saving to file and reading back an array of associative array, in a ruby like way. saying i have something like : one = {"Name" => "Smith",... |
|
|
All times are GMT. The time now is 07:09 AM. | Privacy Policy
|