2011-01-09 2 views
1

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

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 (which comes from [value] in [0] in the entries array)
home (which comes from [value] in [1] in the entries array)

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

спасибо !!

+0

Чтобы уточнить, вы хотите, чтобы '$ client-> response()' возвращал массив типа 'array ('HE RE ',' home ') '? – Kevin

+0

@Kevin: Совершенно верно :) Спасибо за то, что он стал намного яснее – Dov

ответ

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

EDIT: Ваш новый index.php:

include "class_client.php"; 
$client->set('place', 'home'); 
$client->placeLookup(); 
list($status, $name) = $client->response(); 
$client->screen('Status: '.$status.', Name: '.$name); 
+0

@Kevin: Ошибка синтаксиса: неожиданный T_FUNCTION, ожидающий ')' – Dov

+0

Я дам вам альтернативу, но из любопытства, какая версия PHP вы на? – Kevin

+0

@Kevin: 5.2.12 - это, вероятно, объясняет проблему .. – Dov