5

I'm calling the script at: http://phat-reaction.com/googlefonts.php?format=php

And I need to convert the results into a PHP array format like the one I'm currently hard coding:

$googleFonts = array(
    "" => "None",
    "Abel"=>"Abel",
    "Abril+Fatface"=>"Abril Fatface",
    "Aclonica"=>"Aclonica",
    etc...
    );

The php returned is serialized:

a:320:{
    i:0;
    a:3:{
        s:11:"font-family";
        s:32:"font-family: 'Abel', sans-serif;";
        s:9:"font-name";
        s:4:"Abel";
        s:8:"css-name";
        s:4:"Abel";
        }
    i:1;
    a:3:{
        s:11:"font-family";
        s:38:"font-family: 'Abril Fatface', cursive;";
        s:9:"font-name";
        s:13:"Abril Fatface";
        s:8:"css-name";
        s:13:"Abril+Fatface";
        }

        etc...

How can I translate that into my array?

1
  • 2
    That's quite simply PHPs serialization format. Use unserialize() to turn it back. Commented Dec 17, 2011 at 18:38

2 Answers 2

8

You can do this by unserializing the data (using unserialize()) and then iterating through it:

$fonts = array();

$contents = file_get_contents('http://phat-reaction.com/googlefonts.php?format=php');
$arr = unserialize($contents);

foreach($arr as $font)
{
    $fonts[$font['css-name']] = $font['font-name'];
}

Depending on what you're using this for, it may be a good idea to cache the results so you're not fetching external data each time the script runs.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 thanks for the help! Asking in a separate question about how to cache the results.
Here's my related question if you are interested in checking it out > stackoverflow.com/questions/8547253/…
1

Use unserialize(): http://www.php.net/unserialize

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.