带字符串的遍历数组结构

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];

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