donk68254 2014-09-04 21:13
浏览 39
已采纳

带字符串的遍历数组结构

Can I store a pre-made array traversal?

I want to store several API calls, and also how I get to the relevant information from their response.

For example:

$url = 'http://maps.googleapis.com/maps/api/elevation/json?locations='.$location->$latitude.','.$location->$longitude.'&sensor=true';

$response = json_decode(file_get_contents($url), true);

$result = $response['results'][0]['elevation'];

Can I save this part as a string, for storage in my DB or a variable:

$elevation = "['results'][0]['elevation']";

Then later somehow use it to parse the response, ie.

$result = $response[$elevation];
  • 写回答

1条回答 默认 最新

  • dtwvr26066 2014-09-04 21:36
    关注

    The answer is no, sorry ! you will need to store your $response as it is and call it later on using the correct format $response['results'][0]['elevation']

    You may however want to use serialize() if the problem is about how to persist the array into your database:

    $db->insert(serialize($reponse));
    

    then when you retrieve the response from your db use unserialize:

    $response=unserialize($db->fetchReponse());
    $elevation=$response['results'][0]['elevation'];
    

    EDIT

    Based on your comment below it seems what you need is a Cache. Whereby prior to sending the request to the web service API, your application checks in a cache to see if you already have the data available locally. As above example you would most likely want to serialize the PHP array or simply cache the raw response, given that it is in JSON format (PHP serialization will create something very similar anyway). You would create the Cache key from the query params : location, etc.

    Your cached object can be stored in a DB if you choose or on the file system, or even in Memory.

    Check out ZF2 Cache component :

    http://framework.zend.com/manual/2.0/en/modules/zend.cache.storage.adapter.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题