Arrays out of a file and back in?

View: New views
7 Messages — Rating Filter:   Alert me  

Arrays out of a file and back in?

by Leam Hall :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Down the road I'll get this going in a database, but at the moment I'm
stuck using flat files to store multi-dimensional array data. So I have
a file that gets imported, is used and potentially changed, and then
needs to be written back to a file if any data changed.

Is there a good way to do this other than just iterate through each array?

Thanks!

Leam
_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by Allen Shaw-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Leam Hall wrote:
> Down the road I'll get this going in a database, but at the moment I'm
> stuck using flat files to store multi-dimensional array data. So I
> have a file that gets imported, is used and potentially changed, and
> then needs to be written back to a file if any data changed.
>
> Is there a good way to do this other than just iterate through each
> array?
If by "do this" you mean to see if the array changed, what about
array_diff()?

--
Allen Shaw
TwoMiceAndAStrawberry.com

"Data Management, Web Applications, and the Meaning of Life"

allen@...
Phone: (903)361-7429 ("361-SHAW")
Fax:   (253)276-8711
http://www.TwoMiceAndAStrawberry.com

_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by Glenn Powell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This might do it.

I haven't used this, but I've seen other's use it.

http://us2.php.net/manual/en/function.serialize.php

HTH,

Glenn

On Oct 20, 2009, at 6:05 PM, Leam Hall wrote:

> Down the road I'll get this going in a database, but at the moment  
> I'm stuck using flat files to store multi-dimensional array data. So  
> I have a file that gets imported, is used and potentially changed,  
> and then needs to be written back to a file if any data changed.
>
> Is there a good way to do this other than just iterate through each  
> array?
>
> Thanks!
>
> Leam
> _______________________________________________
> New York PHP Users Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/Show-Participation

_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by David Krings :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Leam Hall wrote:
> Down the road I'll get this going in a database, but at the moment I'm
> stuck using flat files to store multi-dimensional array data. So I have
> a file that gets imported, is used and potentially changed, and then
> needs to be written back to a file if any data changed.
>
> Is there a good way to do this other than just iterate through each array?

Having never faced this issue this sounds like a job for an XML file and XML
parser. My bet is that there are bazillion of those and they all do the job
slightly differently or not at all.
I guess I leave you with this thought of my amateurish spirit, maybe it sparks
some ideas.

David
_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by Daniel Convissor-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Heya:

> If by "do this" you mean to see if the array changed, what about  
> array_diff()?

And be aware that the order of arguments impacts the results.

$a = array(
    'a' => 'one',
    'b' => 'two',
    'd' => 'four',
);

$b = array(
    'a' => 'one',
    'b' => 'two',
    'c' => 'three',
);

print_r(array_diff($a, $b));
print_r(array_diff($b, $a));

--Dan

--
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
            data intensive web and database programming
                http://www.AnalysisAndSolutions.com/
 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by Tim Lieberman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Oct 20, 2009, at 6:05 PM, Leam Hall wrote:

> Down the road I'll get this going in a database, but at the moment  
> I'm stuck using flat files to store multi-dimensional array data. So  
> I have a file that gets imported, is used and potentially changed,  
> and then needs to be written back to a file if any data changed.
>
> Is there a good way to do this other than just iterate through each  
> array?

Sounds like a job for json_encode/json_decode.

You could use serialize(), but serialize() can have problems with  
multibyte characters.

json_[de|en]code will create nice, concise, strings.

The advantage of serialize() over the json functions is that serialize  
can serialize objects, and you'll actually get them back (assuming the  
class has been defined when you call unserialize()

-Tim

_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation

Re: Arrays out of a file and back in?

by Marc Antony Vose :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Le Oct 21, 2009 à 9:42 AM, Tim Lieberman a écrit :

> On Oct 20, 2009, at 6:05 PM, Leam Hall wrote:
>
>> Down the road I'll get this going in a database, but at the moment  
>> I'm stuck using flat files to store multi-dimensional array data.  
>> So I have a file that gets imported, is used and potentially  
>> changed, and then needs to be written back to a file if any data  
>> changed.
>>
>> Is there a good way to do this other than just iterate through each  
>> array?
>
> Sounds like a job for json_encode/json_decode.
>
> You could use serialize(), but serialize() can have problems with  
> multibyte characters.
>
> json_[de|en]code will create nice, concise, strings.
>
> The advantage of serialize() over the json functions is that  
> serialize can serialize objects, and you'll actually get them back  
> (assuming the class has been defined when you call unserialize()
>

Wanted to follow-up.  If you don't need the object serializing  
capability (which you don't). I'd use JSON also, because serialized  
text files aren't necessarily portable across machines (like if you  
move your application from one server to another).

Cheers,
Marc



_______________________________________________
New York PHP Users Group Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk

http://www.nyphp.org/Show-Participation