gpt4 book ai didi

php - 如何将此字符串反序列化为键=>值对的PHP数组?

转载 作者:可可西里 更新时间:2023-11-01 13:48:05 26 4
gpt4 key购买 nike

我正在调用脚本:http://phat-reaction.com/googlefonts.php?format=php

我需要将结果转换成 PHP 数组格式,就像我目前正在硬编码的格式一样:

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

返回的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...

我怎样才能将它转换成我的数组?

最佳答案

您可以通过反序列化数据(使用 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'];
}

根据您的用途,缓存结果可能是个好主意,这样您就不会在每次脚本运行时都获取外部数据。

关于php - 如何将此字符串反序列化为键=>值对的PHP数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8547009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com