循环遍历Arrays以构建新的数据数组

时间:2015-03-27 11:12:13

标签: php arrays

我的网站上有一个通知部分,我从表中获取数据。可以将子通知分配给其父级,因此我在表中有parent_id,如下所示:

enter image description here

以下是我得到的数组

Array
    (
    [0] => stdClass Object
        (
            [user_title] => Mr
            [user_firstname] => Richard
            [user_lastname] => Knight-Gregson
            [assignee_title] => Mr
            [assignee_firstname] => Richard
            [assignee_lastname] => Knight-Gregson
            [created] => 2015-03-26 11:40:20
            [id] => 61
            [user_id] => 88
            [description] => message for user rkg uid 88
            [assignee] => 88
            [parent_id] => 
            [cc] => 
            [bcc] => 
            [status] => 0
        )

[1] => stdClass Object
    (
        [user_title] => Mr
        [user_firstname] => Richard
        [user_lastname] => Knight-Gregson
        [assignee_title] => Mr
        [assignee_firstname] => Richard
        [assignee_lastname] => Knight-Gregson
        [created] => 2015-03-26 12:45:47
        [id] => 71
        [user_id] => 88
        [description] => This is child
        [assignee] => 88
        [parent_id] => 61
        [cc] => 
        [bcc] => 
        [status] => 0
    )

[2] => stdClass Object
    (
        [user_title] => Mr
        [user_firstname] => Ted
        [user_lastname] => Chiles
        [assignee_title] => Mr
        [assignee_firstname] => Richard
        [assignee_lastname] => Knight-Gregson
        [created] => 2015-03-27 10:11:56
        [id] => 207
        [user_id] => 47
        [description] => cc 88
        [assignee] => 88
        [parent_id] => 
        [cc] => 88
        [bcc] => 112
        [status] => 0
    )

[3] => stdClass Object
    (
        [user_title] => Mr
        [user_firstname] => Richard
        [user_lastname] => Knight-Gregson
        [assignee_title] => Mr
        [assignee_firstname] => Richard
        [assignee_lastname] => Knight-Gregson
        [created] => 2015-03-27 10:17:31
        [id] => 209
        [user_id] => 88
        [description] => sadfasdfasd
        [assignee] => 88
        [parent_id] => 
        [cc] => 88
        [bcc] => 0
        [status] => 0
    )

 )

这是我目前循环遍历数组的代码,我需要添加的内容是获取所有具有parent_id的元素,然后将它们显示在列表中的repsective parent下...

<table class="notifications-table" border="1">
        <tr>
            <th>S No.</th><th>Date</th><th>Employee</th><th id="details">Details</th><th>Assignee</th><th>Action</th>
        </tr>

    <?php foreach($notifications as $row){ ?>

        <?php foreach($row as $k){


        } ?>

        <tr <?php if($row->status){ echo "class='resolved'"; } ?> id="row-<?=$row->id;?>">

            <td><?=$row->user_id?></td><td><?=date('d M, H:i',strtotime($row->created));?></td>

            <td><?=$row->user_title.' '.$row->user_firstname.' '.$row->user_lastname;?></td>

            <td>
                row id is : <?=$row->id.' == '.$row->parent_id; ?>
                <?php //echo $row->id.' == '.$row->parent_id; ?>
                <?=$row->description; ?><span data-parent-id="<?=$row->id;?>" data-user-id="<?=$row->user_id;?>" id="add_not" data-featherlight="#send_child_message" data-id="add_child_notification" class="glyphicon glyphicon-plus"></span>

            </td>

            <td><?=$row->assignee_title.' '.$row->assignee_firstname.' '.$row->assignee_lastname;?></td>

            <td><input <?php if($row->status){ echo "disabled"; } ?> value="<?=$row->id;?>" data-action="not" type="checkbox" /></td>

        </tr>

    <?php } ?>

</table>

我尝试了一些东西,但却无法得到我想要的东西! 此致!

1 个答案:

答案 0 :(得分:0)

enter image description here

您拥有的代码似乎是正确的。我能够占用你的大部分结构并用它来迭代它并得到你在截图中看到的内容:

我能够看到2行信息,因为我只使用了2个元素(不是全部)。

好的,我现在看到你要做的事情。您想要显示父ID元素的详细信息 - 在示例中,它将是我的“第2行”,因为行ID 71是'行'的第6行的子项。

如果我的假设是正确的,你需要根据父母和父母的这个想法将元素相互关联起来。子。

在“详细信息”关闭td之前,您可以添加...

<?php foreach($notifications as $row2){ ?>
  <?php if ($row2->id == $row->parent_id) : ?> PARENT DESC: <?= $row2->description; ?><?php endif; ?> 
<?php } ?>

现在应该允许您提取一些父属性值。 enter image description here