可能重复:
Access array returned by a function in php我必须通过foreach访问此值吗?
我在返回数据的单个项目的阵列的类的功能。
public function retrieveItemData($item_id) {
$stmt = parent::query("SELECT title FROM `item_main` WHERE `item_id` = $item_id");
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$item = array(
'id' => $item_id,
'title' => $row['title'],
'url' => $item_id . '/' .$this->generate_seo_link($row['title'], '-')
);
endwhile;
return $item;
}
在别处类叫我像这样
$this->return .= '<td>' . $this->retrieveItemData($rep['source']) . '</td>';
$this->retrieveItemData($rep['source'])
功能显然是打印“阵”,我怎么可以从这里访问标题密钥?
我已经试过
$this->retrieveItemData($rep['source'])['title']
而且
$this->retrieveItemData($rep['source'])->title
,但没有运气。
尝试print_r的,这样你可以分析阵列更好 – raygo
我最好的猜测,没有看到父::查询()函数将该提取返回一组行。试试'$ this-> retrieveItemData($ rep ['source'])[0] ['title']'。雷戈是对的。 print_r会给你一个很好的见解。 – tommarshall
我得到语法错误,意外的'['如果我那样做@tommarshall –