如何将此字符串反序列化为key => value对的PHP数组?

时间:2022-02-15 22:09:20

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

我正在调用脚本: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:

我需要将结果转换为PHP数组格式,就像我目前正在编写的那样:

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

The php returned is serialized:

返回的PHP是序列化的:

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?

如何将其转换为我的数组?

2 个解决方案

#1


8  

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

您可以通过反序列化数据(使用unserialize())然后迭代它来完成此操作:

$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.

根据您使用的内容,最好缓存结果,这样每次脚本运行时都不会获取外部数据。

#2


1  

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

使用unserialize():http://www.php.net/unserialize

#1


8  

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

您可以通过反序列化数据(使用unserialize())然后迭代它来完成此操作:

$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.

根据您使用的内容,最好缓存结果,这样每次脚本运行时都不会获取外部数据。

#2


1  

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

使用unserialize():http://www.php.net/unserialize