2015-04-02 104 views
1

特定對象項我用下面的函數,其中$taskTaskData實例獲得來自陣列

var_dump(array_pop($task)); 

// outputs when used repeatdly: 
object(TaskData)[18] 
    private 'id_task' => string '4' (length=1) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[22] 
    private 'id_task' => string '9' (length=1) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[27] 
    private 'id_task' => string '18' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[46] 
    private 'id_task' => string '42' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[18] 
    private 'id_task' => string '47' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[36] 
    private 'id_task' => string '57' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[35] 
    private 'id_task' => string '63' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[41] 
    private 'id_task' => string '68' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

object(TaskData)[26] 
    private 'id_task' => string '70' (length=2) 
    private 'name' => null 
    private 'process_id' => null 

數組我如何可以訪問id_task項目(樣品中:'4','9','18','42')?

+0

是'TaskData'你自己的班級? – 2015-04-02 07:50:49

+0

是的,如果需要,我可以修改它。爲什麼? – Raccoon 2015-04-02 07:52:07

+0

可能的重複[在PHP中,是否有一個函數返回由關聯數組中的一個鍵的值組成的數組?](http://stackoverflow.com/questions/10659875/in-php-is函數返回一個數組由一個鍵值組成) – 2015-04-02 08:03:13

回答

1

$task似乎是一個TaskData實例的數組。您嘗試訪問的成員是私人的。一個read on visibility

private 'id_task' 

,如果它是公共它可以達成這樣:

foreach ($task as $taskData) { 
    var_dump($taskData->id_task); 
} 

you're perfectly right,設置一個公共的getter爲私有成員是一種更好的主意:

public function getTaskID() { 
    return $this->id_task; 
} 
+1

我創建了一個公共函數'public function getTaskID(){return $ this-> id_task;}' 它完美的工作,謝謝! – Raccoon 2015-04-02 08:59:26