如何爆炸这个数组?

时间:2011-01-09 08:42:01

标签: php xml arrays explode

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

的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 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中的代码),也会更喜欢。

谢谢!

1 个答案:

答案 0 :(得分: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);