doutan3192 2015-03-27 11:12
浏览 55

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

I have a notifications section on my site and I am getting data from my table. There can be child notifications assigned to their parent, so I have parent_id in the table as shown below:

enter image description here

Below is the array I am getting

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
    )

 )

Here is the code I have at the moment to loop through the array, what I need to add to this is to get all elements that have a parent_id and then display them under their repsective parent in a list...

<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>

I have tried a few things but just cant get what I want! Regards!

  • 写回答

1条回答 默认 最新

  • doulou1989 2015-03-27 14:21
    关注

    enter image description here

    The code you have seems correct. I was able to take most of your structure and iterate over it with and got what you see in screenshot:

    I was able to see 2 rows of info because I only used 2 elements (not all of them).

    Ok, I see now what you are trying to do. You want to display details of the parent id element - which in the example, would be my "row 2" since row id 71 is a 'child of' row id 61.

    If my assumption is correct, you need to associate the elements with each other based on this idea of parent & child.

    Before your "Details" closing td, you can add...

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

    Which should now let you pull in some parent property values. enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch