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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?