How to explode this array?
I have tried to explode the returned array and it just doesn't want to display. Obviously I am doing something wrong. Here is the code for the bit I am having trouble exploding.
index.php
include "class_client.php";
$client->set('place', 'home');
$client->placeLookup();
$client->screen($client->response()); //want to replace this to print the selected exploded data as shown at the bottom of this question
class_client.php
private $data = array();
private $response = NULL;
public function set($key, $value) {
$this->data[$key] = $value;
return $this;
}
private function get($key) {
return $this->data[$key];
}
public function response() {
return $this->response;
}
public function placeLooku开发者_Go百科p() {
$this->response = $this->srv()->placeLookup(array('place' => $this->get('place')));
return $this;
}
Output
stdClass Object
(
[return] => stdClass Object
(
[fields] => stdClass Object
(
[entries] => Array
(
[0] => stdClass Object
(
[key] => place.status
[value] => HERE
)
[1] => stdClass Object
(
[key] => place.name
[value] => home
)
)
)
[operation] => place.lookup
[success] => TRUE
)
)
The only data I want to see in output on index.php is;
HERE (which comes from [value] in [0] in the entries array)
home (which comes from [value] in [1] in the entries array)
Would also prefer if I can explode within class_client.php and return the values back as the new array to index.php (to minimise/hide the code within index.php).
Thank you!!
Assuming you're on PHP 5.3+, you can replace your response
method with this:
public function response() {
return array_map(function($a) {
return $a->value;
}, $this->response->return->fields->entries);
}
Otherwise, try:
public function response() {
return array_map(array($this, 'getValue'), $this->response->return->fields->entries);
}
public function getValue($obj) {
return $obj->value;
}
EDIT: Your new index.php:
include "class_client.php";
$client->set('place', 'home');
$client->placeLookup();
list($status, $name) = $client->response();
$client->screen('Status: '.$status.', Name: '.$name);
精彩评论