PHP arrays to yaml

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
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

PHP arrays to yaml

Post by shiznatix »

Is there a decent php library that can do array to yaml and yaml to array? I have tried spyc but it only does yaml to php, not the other way around. I have tried to install the php5-yaml package but its only in sid and on our live server it just isn't there and I don't want to start screwing around with that.

So, is there no decent extension or library that can do this?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP arrays to yaml

Post by Benjamin »

I did a quick search and didn't really see anything, although more than likely there is something out there. I'd probably just write something myself in order to do an array to yaml conversion. It would be fairly simple I imagine.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: PHP arrays to yaml

Post by shiznatix »

same, can't find anything on google. Since the arrays that I am converting are quite simple I did just write my own. It looks like this:

Code: Select all

 
    private function _dataToYAML($array)
    {
        $yaml = "---\n";
        foreach ($array as $key => $val)
        {
            $yaml .= ":".$key.": ".$val."\n";
        }
        
        return $yaml;
    }
 
so you know, I guess that works. Still wish there was something better than this.
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Re: PHP arrays to yaml

Post by the_last_tamurai »

I know it's very late, but I just see the topic :)
and maybe it can help someone
see this component http://components.symfony-project.org/yaml/
Post Reply