从数组中获取特定的对象项

时间:2015-04-02 07:26:03

标签: php arrays oop

我正在使用以下函数,其中$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')?

1 个答案:

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