1

我试图爆炸返回的数组,它只是不想显示。显然我做错了什么。这是我在爆炸时遇到问题的代码。

索引.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 placeLookup() {
$this->response = $this->srv()->placeLookup(array('place' => $this->get('place')));
return $this;
}

输出

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
        )

)

我想在 index.php 的输出中看到的唯一数据是;

HERE(来自条目数组中 [0] 中的 [value])
home(来自条目数组 [1] 中的 [value])

如果我可以在 class_client.php 中分解并将值作为新数组返回给 index.php(以最小化/隐藏 index.php 中的代码),我也会更喜欢。

谢谢!!

4

1 回答 1

1

假设您使用的是 PHP 5.3+,您可以将您的response方法替换为:

public function response() {
    return array_map(function($a) {
        return $a->value;
    }, $this->response->return->fields->entries);
}

否则,请尝试:

public function response() {
    return array_map(array($this, 'getValue'), $this->response->return->fields->entries);
}

public function getValue($obj) {
    return $obj->value;
}

编辑:你的新 index.php:

include "class_client.php";
$client->set('place', 'home');
$client->placeLookup();
list($status, $name) = $client->response();
$client->screen('Status: '.$status.', Name: '.$name);
于 2011-01-09T09:43:16.367 回答