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