从数组中提取信息


<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);
}

$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; ?>

请注意:

<?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']

如果您想访问$qnote['Step']中的所有['body'],请使用foreach:

foreach($qnote['Step'] as $step)
{
    echo $step['body'];
}

编辑:仔细一想,还要注意'Step'不是'Qnote'的成员(可能也不是$ Qnote)。

相关内容

  • 没有找到相关文章

最新更新