在 PHP 中从顺序数组创建嵌套数组

我需要在 PHP 中对数组进行排序的帮助。我收到这些数组,如下所示:


Array (

    [0] => Array (

        [0] => Array (

            [id] => 1

            [username] => STLUserFolks

            [event_id] => 7e0a2faa-43c8-4552-b0ec-c4d0fa6c00d1

            [event_on] => 2020-06-29 17:39:25.93432

            [ent_pix] =>

            [\"images/events/STLUserFolks/tammy1.jpeg\",\"images/events/STLUserFolks/tammy2.jpeg\",\"images/events/STLUserFolks/tammy3.jpeg\",\"images/events/STLUserFolks/tammy4.jpeg\",\"images/events/STLUserFolks/tammy5.jpeg\",\"images/events/STLUserFolks/tammy6.jpeg\"]

            [amount] => 20

            [event_title] => Fun on vacation with friends.

            [event_type] => mix

            [event_details] => Saw Great and interesting sites today!

        )

    )

    [1] => Array (

        [0] => Array (

            [id] => 1

            [username] => STLUserFolks

            [event_id] => debd6476-4f24-4d2c-9973-7c36d256079d

            [event_on] => 2020-07-09 05:39:10.588842

            [ent_pix] =>

            [\"images/events/STLUserFolks/680.JPG\",\"images/events/STLUserFolks/681.JPG\",\"images/events/STLUserFolks/682.JPG\"]

            [amount] => 25

            [event_title] => Sexy Warm

            [event_type] => photos

            [event_details] => Feeling the sun on our faces.

        )

        [1] => Array (

            [id] => 1

            [user_id] => 2

            [username] => STLUserFolks

            [member_user] => spiffy_user

            [comment_text] => Amazing! you guys are simply amazing!

            [created_on] => 2020-07-10 15:04:46.480001

            [comment_id] => c803294d-8483-43c9-a76b-4cba56795266

            [event_id] => debd6476-4f24-4d2c-9973-7c36d256079d

        )

    )

    [2] => Array (

        [0] => Array (

            [id] => 1

            [username] => STLUserFolks

            [event_id] => 3112f8ff-6119-48c4-810c-594585b5dc63

            [event_on] => 2020-07-09 07:11:32.840511

            [ent_pix] =>

            

我需要能够循环这些数组以将注释数组([1] 及以上)应用到主数组 [0]。或者我需要 [0] 之后的顺序数组像这样嵌套:


呼唤远方
浏览 129回答 2
2回答

蝴蝶不菲

假设您提供的输入数据位于 data.php 中:<?php$data = include('data.php');$result = [];foreach ($data as $row) {&nbsp; $user = $row[0];&nbsp; foreach ($row as $k => $element) {&nbsp; &nbsp; if ($k === 0) {&nbsp; &nbsp; &nbsp; $user = $element;&nbsp; &nbsp; &nbsp; $user['user_comments'] = [];&nbsp; &nbsp;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; $user['user_comments'][] = $element;&nbsp; &nbsp; }&nbsp; }&nbsp; $result[] = $user;}var_dump($result);结果是:array(3) {&nbsp; [0]=>&nbsp; array(10) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; string(36) "7e0a2faa-43c8-4552-b0ec-c4d0fa6c00d1"&nbsp; &nbsp; ["event_on"]=>&nbsp; &nbsp; string(25) "2020-06-29 17:39:25.93432"&nbsp; &nbsp; ["ent_pix"]=>&nbsp; &nbsp; array(6) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy1.jpeg"&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy2.jpeg"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy3.jpeg"&nbsp; &nbsp; &nbsp; [3]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy4.jpeg"&nbsp; &nbsp; &nbsp; [4]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy5.jpeg"&nbsp; &nbsp; &nbsp; [5]=>&nbsp; &nbsp; &nbsp; string(38) "images/events/STLUserFolks/tammy6.jpeg"&nbsp; &nbsp; }&nbsp; &nbsp; ["amount"]=>&nbsp; &nbsp; int(20)&nbsp; &nbsp; ["event_title"]=>&nbsp; &nbsp; string(29) "Fun on vacation with friends."&nbsp; &nbsp; ["event_type"]=>&nbsp; &nbsp; string(3) "mix"&nbsp; &nbsp; ["event_details"]=>&nbsp; &nbsp; string(38) "Saw Great and interesting sites today!"&nbsp; &nbsp; ["user_comments"]=>&nbsp; &nbsp; array(0) {&nbsp; &nbsp; }&nbsp; }&nbsp; [1]=>&nbsp; array(10) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; string(36) "debd6476-4f24-4d2c-9973-7c36d256079d"&nbsp; &nbsp; ["event_on"]=>&nbsp; &nbsp; string(26) "2020-07-09 05:39:10.588842"&nbsp; &nbsp; ["ent_pix"]=>&nbsp; &nbsp; array(3) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; string(34) "images/events/STLUserFolks/680.JPG"&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(34) "images/events/STLUserFolks/681.JPG"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(34) "images/events/STLUserFolks/682.JPG"&nbsp; &nbsp; }&nbsp; &nbsp; ["amount"]=>&nbsp; &nbsp; int(25)&nbsp; &nbsp; ["event_title"]=>&nbsp; &nbsp; string(9) "Sexy Warm"&nbsp; &nbsp; ["event_type"]=>&nbsp; &nbsp; string(6) "photos"&nbsp; &nbsp; ["event_details"]=>&nbsp; &nbsp; string(29) "Feeling the sun on our faces."&nbsp; &nbsp; ["user_comments"]=>&nbsp; &nbsp; array(1) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; array(8) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(1)&nbsp; &nbsp; &nbsp; &nbsp; ["user_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(2)&nbsp; &nbsp; &nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; &nbsp; &nbsp; ["member_user"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(11) "spiffy_user"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_text"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(37) "Amazing! you guys are simply amazing!"&nbsp; &nbsp; &nbsp; &nbsp; ["created_on"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(26) "2020-07-10 15:04:46.480001"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "c803294d-8483-43c9-a76b-4cba56795266"&nbsp; &nbsp; &nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "debd6476-4f24-4d2c-9973-7c36d256079d"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; [2]=>&nbsp; array(10) {&nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; int(1)&nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; string(36) "3112f8ff-6119-48c4-810c-594585b5dc63"&nbsp; &nbsp; ["event_on"]=>&nbsp; &nbsp; string(26) "2020-07-09 07:11:32.840511"&nbsp; &nbsp; ["ent_pix"]=>&nbsp; &nbsp; array(4) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; string(40) "images/events/STLUserFolks/IMG_2135.JPEG"&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; string(40) "images/events/STLUserFolks/IMG_2136.JPEG"&nbsp; &nbsp; &nbsp; [2]=>&nbsp; &nbsp; &nbsp; string(40) "images/events/STLUserFolks/IMG_2137.JPEG"&nbsp; &nbsp; &nbsp; [3]=>&nbsp; &nbsp; &nbsp; string(40) "images/events/STLUserFolks/IMG_2140.JPEG"&nbsp; &nbsp; }&nbsp; &nbsp; ["amount"]=>&nbsp; &nbsp; int(25)&nbsp; &nbsp; ["event_title"]=>&nbsp; &nbsp; string(9) "New Pants"&nbsp; &nbsp; ["event_type"]=>&nbsp; &nbsp; string(6) "photos"&nbsp; &nbsp; ["event_details"]=>&nbsp; &nbsp; string(32) "Do these jeans make me look big?"&nbsp; &nbsp; ["user_comments"]=>&nbsp; &nbsp; array(2) {&nbsp; &nbsp; &nbsp; [0]=>&nbsp; &nbsp; &nbsp; array(8) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(1)&nbsp; &nbsp; &nbsp; &nbsp; ["user_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(1)&nbsp; &nbsp; &nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; &nbsp; &nbsp; ["member_user"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(8) "Some.Guy"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_text"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(17) "Wish I was there!"&nbsp; &nbsp; &nbsp; &nbsp; ["created_on"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(26) "2020-07-10 10:45:42.809338"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "19115a3e-87ca-410c-aade-c47122068bca"&nbsp; &nbsp; &nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "3112f8ff-6119-48c4-810c-594585b5dc63"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; [1]=>&nbsp; &nbsp; &nbsp; array(8) {&nbsp; &nbsp; &nbsp; &nbsp; ["id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(1)&nbsp; &nbsp; &nbsp; &nbsp; ["user_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; int(2)&nbsp; &nbsp; &nbsp; &nbsp; ["username"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(12) "STLUserFolks"&nbsp; &nbsp; &nbsp; &nbsp; ["member_user"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(11) "spiffy_user"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_text"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(27) "Looks like an amazing time."&nbsp; &nbsp; &nbsp; &nbsp; ["created_on"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(26) "2020-07-10 15:09:05.275935"&nbsp; &nbsp; &nbsp; &nbsp; ["comment_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "bfd4d4e3-1c88-4f03-936f-cd456dba6096"&nbsp; &nbsp; &nbsp; &nbsp; ["event_id"]=>&nbsp; &nbsp; &nbsp; &nbsp; string(36) "3112f8ff-6119-48c4-810c-594585b5dc63"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }}那是你要的吗?与您预期结果的唯一区别是,每一行都有“user_comments”数组,该数组可以为空,有一个或多个元素与此属性丢失或被调用或user_comment如user_comments您的示例中所示。我相信这更加一致且更易于以编程方式使用,但如果需要,它也可以轻松调整为您发布的内容。编辑评论和点赞。如果您想同时填写 user_comments 和 user_likes,则必须检测某些数据是否是评论或点赞。我这样做是这样的:如果数据包含comment_id,它是一条评论,如果它有like_id,它是一个喜欢。这是下面附加的简单编辑。片段变成:foreach ($row as $k => $element) {&nbsp; &nbsp; if ($k === 0) {&nbsp; &nbsp; &nbsp; $user = $element;&nbsp; &nbsp; &nbsp; $user['user_comments'] = [];&nbsp; &nbsp; &nbsp; $user['user_likes'] = [];&nbsp; &nbsp; } else if (isset($element['comment_id'])) {&nbsp; &nbsp; &nbsp; $user['user_comments'][] = $element;&nbsp; &nbsp; } else if (isset($element['like_id'])) {&nbsp; &nbsp; &nbsp; $user['user_likes'][] = $element;&nbsp; &nbsp; }&nbsp; }如果需要,您稍后可以通过这种方式扩展它来处理不同的事件。

开满天机

我使用该comment_text键来过滤给定线程的评论和非评论,并将所有内容分组user_comments到主线程下。我使用array_map将此过滤器应用于列表中的每个线程。$flatThreads = \\your initial value$nestedThreads = array_map(function($thread){&nbsp; $notComments = array_filter($thread, function($thread_item){&nbsp; &nbsp; return !isset($thread_item["comment_text"]);&nbsp; });&nbsp; $comments = array_filter($thread, function($thread_item){&nbsp; &nbsp; return isset($thread_item["comment_text"]);&nbsp; });&nbsp; $mainItem = reset($notComments); //get first item that is not a comment&nbsp; $mainItem["user_comments"] = $comments;&nbsp; return $mainItem;&nbsp;&nbsp;}, $flatThreads);var_dump($nestedThreads); //what you requested
打开App,查看更多内容
随时随地看视频慕课网APP