如何反序列化这个字符串转换成键的PHP数组=>值对? [英] How to deserialize this string into a PHP array of key => value pairs?

查看:172
本文介绍了如何反序列化这个字符串转换成键的PHP数组=>值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用脚本googlefonts.php?格式= PHP

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

和我需要的结果转化为像一个我目前硬编码一个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...
    );

返回的PHP是序列化的:

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?

推荐答案

您可以通过反序列化的数据(使用 反序列化做到这一点() ),然后通过它进行迭代:

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.

这篇关于如何反序列化这个字符串转换成键的PHP数组=>值对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆