简单PHP缓存-可以在数组中存储数据,但如何从该数组缓存中检索项?
我已经能够使用这个缓存类的store函数,但是我无法从存储的数组中检索某个项简单PHP缓存-可以在数组中存储数据,但如何从该数组缓存中检索项?,php,arrays,caching,store,Php,Arrays,Caching,Store,我已经能够使用这个缓存类的store函数,但是我无法从存储的数组中检索某个项 $c->setCache("$appid") ->store("$appid", array( 'name' => "$gname", 'price' => "$price", 'logo' => "$glogo", 'link' => "$glink" ) ); $gcac
$c->setCache("$appid")
->store("$appid", array(
'name' => "$gname",
'price' => "$price",
'logo' => "$glogo",
'link' => "$glink"
)
);
$gcache = $c->retrieve("$appid");
$cprice = $gcache->price; //not sure how to retrieve the price value only
以下是我无法从存储阵列中获取价格的完整代码:
<?php
require_once 'cache.class.php';
// Setup 'default' Cache
$c = new Cache();
$glist = "http://steamcommunity.com/id/aksn1p3r/games?tab=all&xml=1";
$gxml = simplexml_load_file($glist);
$tprice = 0;
$i = 0; //just used to exit loop after 2 executions
foreach($gxml->games->game as $game) {
if($i >= 2) {
break;
}
$appid = $game->appID;
$glink = $game->storeLink;
$gname = $game->name;
$glogo = $game->logo;
$gjson = 'http://store.steampowered.com/api/appdetails/?appids=' . $appid;
$fgc = file_get_contents($gjson);
$jd = json_decode($fgc, true);
$gdata = $jd[intval($appid)]['data']; //$appid needs to be integer here, not string
$gdesc = $gdata['about_the_game'];
$gprice = $gdata['price_overview']['final'];
$price = number_format($gprice / 100, 2);
$c->setCache("$appid")
->store("$appid", array(
'name' => "$gname",
'price' => "$price",
'logo' => "$glogo",
'link' => "$glink"
)
);
// this part is where I dont understand how to get the price I stored.
$cprice = $c->retrieve["$appid"]['price'];
$tprice += $cprice;
$i++;
}
echo 'Total games: ' .$i. ' <br>';
echo 'Total Price: ' .$tprice;
?>
games->games as$game){
如果($i>=2){
打破
}
$appid=$game->appid;
$glink=$game->storeLink;
$gname=$game->name;
$glogo=$game->logo;
$gjson=http://store.steampowered.com/api/appdetails/?appids=“.$appid;
$fgc=文件获取内容($gjson);
$jd=json_decode($fgc,true);
$gdata=$jd[intval($appid)]['data'];//$appid在这里必须是整数,而不是字符串
$gdesc=$gdata[“关于游戏”];
$gprice=$gdata['price_overview']['final'];
$price=number_格式($gprice/100,2);
$c->setCache($appid)
->存储(“$appid”),数组(
'name'=>“$gname”,
“价格”=>“$price”,
“徽标”=>“$glogo”,
'链接'=>“$glink”
)
);
//这部分是我不知道如何获得我存储的价格的地方。
$cprice=$c->retrieve[“$appid”]['price'];
$tprice+=$cprice;
$i++;
}
echo“总游戏数:”.$i.'
';
echo‘总价:’。$t价格;
?>
您正在存储数组
希望获得对象
替换
$gcache->price;
与
如果您能向我们展示您当前的工作代码,将对您和我们都有好处。。您无法检索的内容、嵌套的内容等。您好,是的,对不起,我正在编辑它。如果您进行var_转储($cprice);打印的是什么?@AK您是否按照文档进行了操作。。它列出了如何从返回的数组中检索数据。@DarylGill我已经尝试通过调用数组名来检索缓存的数据,它从该数组中提取存储的所有数据。我在文档中找不到如何从数组中提取单个项目的数据,在我的例子中是价格;
$gcache['price'];