Array from file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Array from file

Post by Chris Corbyn »

HI,
I've got a txt file that contains something like this:
"blue_red" => "chips_chocolate", "orange_yellow" => "bananas_grapes", "pink_green" => "cakes_biscuits",
This is what i would like to create an array from. So the first key would be ['blue_red'] with a value "chips_chocolate".

Is there a way to do this?

I tried

Code: Select all

$array = array (file_get_contents('arraycontents.txt'));
print_r($array);
But the output was
Array ( [0] => "blue_red" => "chips_chocolate", "orange_yellow" => "bananas_grapes", "pink_green" => "cakes_biscuits", )
It's just taking the entire lot as key[0].

Thanks :-)
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

could you use serialize() to create the file and unserialize() to reconstruct the array? Or is this something you do not create...
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i think you should read the file and take all keys and values seperately..

i meant:

array.txt

KEY1 VALUE1
KEY2 VALUE2
.
.
.
.

like this...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

mudkicker, could you please explain a bit better?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ok, been looking at serialize. It looks promising but I'm not quite sure how i would put it into action. The file is a file that I create yes so technically I could do that.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

well if you can, preapare a text file like this

key1#value1
key2#value2
key3#value3
...etc

every line has a key and a value seperated by a #.

so do that :

Code: Select all

<?
$array = array();
$file = file("array.txt");
foreach ($file as $f) {
$buf = explode("#",$f);
$array[$f[0]] = $f[1];
}
?>
i didnt test it but it should work..
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

first explode it by comma then explode those by => then build your array

http://us3.php.net/manual/en/function.explode.php

like

Code: Select all

<?php

$indeces = explode(",", file_get_contents('arraycontents.txt')); 
$array = array();

foreach($indeces as $index){
  $temp = explode(" => ", $index);
  $array["$temp[0]"] = "$temp[1]";
}

?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

magicrobotmonkey, explode() did the trick. thanks. :-)
Post Reply