2012-02-09 3 views
0

私はTwitterクラスを作成し、そのクラスのメソッドとプロパティを呼び出すオブジェクトを作成しようとしています。本質的に私がやっているのは、Twitterユーザー名のデータベースを呼び出して、その結果をsimplexmlリクエストすることです。 (コードの部分は省略しています)。フル配列が返されないPHP

return $this->posts最初の配列の項目だけが返されるのはなぜ分かりませんが、すべてが正常に機能しているようです。 returnを削除すると、配列全体が返されます。下のオブジェクトにprint_rを使ってテストしています。

<?php 
    class twitter { 
     public $xml; 
     public $count; 
     public $query; 
     public $result; 
     public $city; 
     public $subcategory; 
     public $screen_name; 
     public $posts; 

     public function arrayTimeline(){ 
      $this->callDb($this->city, $this->subcategory); 
      while($row = mysql_fetch_row($this->result)){ 
       foreach($row as $screen_name){ 
        $this->getUserTimeline($screen_name, $count=2); 
       } 
       foreach($this->xml as $this->status){ 
        return $this->posts[] = array("image"=>(string)$this->status->user->profile_image_url,"name"=>(string)$this->status->name, "username"=>(string)$this->status->user->name, "text"=>(string)$this->status->text, "time"=>strtotime($this->status->created_at)); 
       } 
      } 
     } 


    $test = new twitter; 
    $test->city="phoenix"; 
    $test->subcategory="computers"; 

    $test->arrayTimeline(); 

    print_r($test->posts); 

    ?> 

答えて

5

これは、returnによってPHPが現在呼び出しているメソッドを終了するためです。ループからリターンを移動すると、完全な配列が得られます。

public function arrayTimeline(){ 
     $this->callDb($this->city, $this->subcategory); 
     while($row = mysql_fetch_row($this->result)){ 
      foreach($row as $screen_name){ 
       $this->getUserTimeline($screen_name, $count=2); 
      } 
      foreach($this->xml as $this->status){ 
       $this->posts[] = array("image"=>(string)$this->status->user->profile_image_url,"name"=>(string)$this->status->name, "username"=>(string)$this->status->user->name, "text"=>(string)$this->status->text, "time"=>strtotime($this->status->created_at)); 
      } 
     } 

     return $this->posts; 
    } 
関連する問題