View Full Version : Can I do this??
shachi
08-27-2006, 07:18 AM
Hello all,
Can anyone tell me if I can read a file into an array with the file() function as key and value??
Let me demonstrate:
My file looks something like this:
test1|test2
test3|test4
test4|test5
Now I want it to be read as:
$f['test1'] = test2
$f['test3'] = test4
$f['test5'] = test6
Instead of:
$f[0] = test1|test2
$f[2] = test3|test4
$f[3] = test5|test6
Any ideas??
blm126
08-27-2006, 03:29 PM
Do you have to have the file exactly like that. If not take a look at parse_ini_file (http://us2.php.net/parseinifile).
Also note file() will NOT return
$f[0] = test1|test2
$f[2] = test3|test4
$f[3] = test5|test6
it will return
$f[0] = test1|test2\n
$f[2] = test3|test4\n
$f[3] = test5|test6\n
$f[0] = test1|test2\n
$f[2] = test3|test4\n
$f[3] = test5|test6\nIt won't return that, either. It'll return:
$f[0] = "test1|test2\n";
$f[1] = "test3|test4\n";
$f[2] = "test5|test6\n"; :)
$lines = file($filename);
$config = array();
for($i = 0; $i < count($lines); ++$i)
$config[
substr($lines[$i], 0, strpos($lines[$i], '|'))
] =
substr($lines[$i], strpos($lines[$i], '|') + 1, -1); // -2 for a Windows host
shachi
08-27-2006, 05:29 PM
blm126: I don't think I was searching for something like that. I needed the value before the pipe character to be the key and the second after the pipe character to be the value of that array. Thank you for pointing out my mistake.:)
Twey: Will it exactly read as the following??
$lines['test1'] = 'test2';
for a file which is:
test1|test2
shachi
08-27-2006, 06:01 PM
Oh thanks Twey!!!
blm126
08-27-2006, 07:02 PM
blm126: I don't think I was searching for something like that. I needed the value before the pipe character to be the key and the second after the pipe character to be the value of that array.
Parse_ini_file is almost what you want, I think. If you have to use the pipe for the separator it will not work. However, if you can change | to = parse_ini_file is perfect. Take a look at this
data.ini
test = test2
test3 = test4
test.php
print_r(parse_ini_file('data.ini'));
will output
Array
(
[test] => test2
[test3] => test4
)
I think. I didn't test it(not testing is normally followed by someone proving me wrong, and me looking like an idiot.)
shachi
08-27-2006, 07:06 PM
blm126: Yes it is almost what I need but not exactly what I need. I can't change the | to = as I have several places to change if I do so, but I think that now that I know it I may use parse_ini_file() for these purposes.
Thanks blm126 for helping me with it(and teaching me how to use a new PHP function).
shachi
08-29-2006, 04:25 PM
Twey, this is how I must use the function isn't it??
<?php
function newfile($filename){
$lines = file($filename);
$config = array();
for($i = 0; $i < count($lines); ++$i)
$config[substr($lines[$i], 0, strpos($lines[$i], '|'))] = substr($lines[$i], strpos($lines[$i], '|') + 1, -1); // -2 for a Windows host
}
print_r(newfile('newfile.txt'));
?>
or it is something else?? Well if it is this way then why isn't this showing anything??
Because you put it in a function. If you want to do that, add "return $config;" at the end of the function.
shachi
08-29-2006, 04:29 PM
Oh, ok let me try that. Thanks.
shachi
08-29-2006, 04:29 PM
Oh, yes it worked ... Thanks again Twey.:D
By the way if the file is separated by , then do I just replace the | with , ??
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.