Как взорвать этот массив? - PullRequest
       6

Как взорвать этот массив?

1 голос
/ 09 января 2011

Я пытался взорвать возвращенный массив, и он просто не хочет отображаться.Очевидно, я делаю что-то не так.Вот код для бита, который я испытываю при взрыве.

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, это:

ЗДЕСЬ (что происходит от [value] в [0] в массиве entry)
home (который получается из [value] в [1] в массиве записей)

Также предпочел бы, чтобы я мог взорваться в class_client.php и вернуть значения обратно в виде нового массива вindex.php (чтобы минимизировать / скрыть код в index.php).

Спасибо !!

1 Ответ

1 голос
/ 09 января 2011

Если вы используете 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);
...