<pre class="cake-debug">
Array
(
[0] => Array
(
[Qnote] => Array
(
[id] => 67
[subject] => Call Closing
[body] => Gave Information
[user_id] => 39
)
[Step] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 39
[qnote_id] => 67
[type] => a
[body] => This is a text
)
[1] => Array
(
[id] => 3
[user_id] => 39
[qnote_id] => 67
[type] => a
[body] => This is userid 39
)
)
)
)
嗨,我正在尝试从此数组中提取数据
<?php foreach($data as $qnote): ?>
<?php echo $qnote['Qnote']['id']; ?> // Works
<?php echo $qnote['Qnote']['body']; ?> //Works
<?php echo $qnote['Step']['body']; ?> // Doesnt Work ????
<?php endforeach; ?>
提取步骤数据的正确方法是什么。 有人可以指出我正确的方向。
控制器代码:
function view($id = null) {
$this->Qnote->id = $id;
$userID = $this->Authsome->get('id');
$this->paginate = array(
'conditions' => array(
'Qnote.id LIKE' => $id),
);
$data = $this->paginate('Qnote');
$this->set('data', $data);
}
答案 0 :(得分:2)
$qnote['Step']
也是一个多维数组,所以你也需要循环遍历它:
<?php foreach ($data as $qnote) : ?>
<p>Id: <?php echo $qnote['Qnote']['id']; ?></p>
<p>Body: <?php echo $qnote['Qnote']['body']; ?></p>
<p>
Steps:
<ul>
<?php foreach ($qnote['Step'] as $step) : ?>
<li><?php echo $step['body']; ?></li>
<?php endforeach; ?>
</ul>
</p>
<?php endforeach; ?>
答案 1 :(得分:0)
记下:
<?php echo $qnote['Step']['body']; ?> // Doesnt Work ????
其中$qnote['Step']
是:
[Step] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 39
[qnote_id] => 67
[type] => a
[body] => This is a text
)
[1] => Array
(
[id] => 3
[user_id] => 39
[qnote_id] => 67
[type] => a
[body] => This is userid 39
)
)
没有$qnote['Step']['body']
。但是有$qnote['Step'][1]['body']
。
如果您想访问['body']
中的所有$qnote['Step']
,请使用foreach
:
foreach($qnote['Step'] as $step)
{
echo $step['body'];
}
编辑:第二个想法,还要注意'Step'不是'Qnote'的成员(可能不是$ qnote。)你可能想先解决这个问题。