DaveyK Posted March 20, 2013 Share Posted March 20, 2013 Hey freakers, I am stumbling upon a problem I dont know how to solve, so here it is. Its something of a luxery problem, even if I cant fix it I dont mind. However, I would like to know if its possible. What I would like is this. I have this array: Array ( [0] => Array ( [comment_id] => 1 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 0 [time] => March 19th, 5:24AM [content] => Some text ) [1] => Array ( [comment_id] => 2 [user] => Array ( [id] => 2 [name] => Jason Biondo [avatar] => /assets/img/user_images/2_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:24AM [content] => Some text ) [2] => Array ( [comment_id] => 5 [user] => Array ( [id] => 3 [name] => Rudolph [avatar] => /assets/img/user_images/3_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:45AM [content] => Some text ) [3] => Array ( [comment_id] => 6 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:45AM [content] => Some text ) [4] => Array ( [comment_id] => 4 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 2 [time] => March 19th, 5:25AM [content] => Some text ) [5] => Array ( [comment_id] => 3 [user] => Array ( [id] => 3 [name] => Rudolph [avatar] => /assets/img/user_images/3_avi.jpg ) [parent_id] => 0 [time] => March 19th, 5:25AM [content] => Some text ) [6] => Array ( [comment_id] => 7 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 0 [time] => March 19th, 7:47AM [content] => Some text ) ) This is an array of comments on a blog. Regardless, I would like to focus this area in specific: Array ( [0] => Array ( [comment_id] => 1 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 0 [time] => March 19th, 5:24AM [content] => Some text ) [1] => Array ( [comment_id] => 2 [user] => Array ( [id] => 2 [name] => Jason Biondo [avatar] => /assets/img/user_images/2_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:24AM [content] => Some text ) [2] => Array ( [comment_id] => 5 [user] => Array ( [id] => 3 [name] => Rudolph [avatar] => /assets/img/user_images/3_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:45AM [content] => Some text ) [3] => Array ( [comment_id] => 6 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 1 [time] => March 19th, 5:45AM [content] => Some text ) [4] => Array ( [comment_id] => 4 [user] => Array ( [id] => 1 [name] => Davey Kulter [avatar] => /assets/img/user_images/1_avi.jpg ) [parent_id] => 2 [time] => March 19th, 5:25AM [content] => Some text ) ) as you can see from parent_id, the first is a comment and the rest are replies to that comment (I only allow one level of replies, so no reply to a reply). However, if I loop through this array in a foreach() loop and I am at array key [4], how do I find out what the comment_id of the previous array key where parent_id = 0. SO, what I would like is a function that would, in this case, return id = 1. If I dont make sense, I am sorry. Its quite weird xD Thanks regardless Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/ Share on other sites More sharing options...
requinix Posted March 20, 2013 Share Posted March 20, 2013 Returns id=1 because that's the top comment at the hierarchy of replies? Before your foreach loop do another, separate foreach loop. Build an array of comment_id => array key (so it looks like 1=>0, 2=>1, 5=>2, etc). With that array built up ahead of time, you can be on item [4] with parent_id=2, look up [2] in that first array, get 1, grab the parent_id of [1] = 1, look up [1] in that first array, get 0, grab the parent_id of [0] = [0], and stop. You could do that with another loop for (starting with the current item; while the current item has a parent; move to the parent)(where "move to the parent" is a couple nested array references) or do it recursively in a separate function. Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1419760 Share on other sites More sharing options...
DaveyK Posted March 20, 2013 Author Share Posted March 20, 2013 Well, I tried to follow your logic but I did it a little differently. I wrote this function: function find_parent_id_0 ($k, $comments, $array2) { $parent = $comments[$k]['parent_id']; if ($parent == 0) { return $comments[$k]['comment_id']; } else { $prev = prev($comments); $parent = $prev['parent_id']; if ($parent != 0) { $id = find_parent_id_0 (key($comments), $comments, $array2); } else { $id = $prev['comment_id']; } } return $id; } I also made that array you suggested like this: $array2 = array(); foreach ($comments as $k => $comment) { $array2[$comment['comment_id']] = $k; } but Im not using it >.< It appears to work though. any comments? Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1419764 Share on other sites More sharing options...
requinix Posted March 20, 2013 Share Posted March 20, 2013 $prev = prev($comments); I can't tell what you're trying to do with this. $parent is a comment ID. Use $array2 to find the array key in $comments, then find the parent ID there. else { return find_parent_id_0($array2[$parent], $comments, $array2); } Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1419872 Share on other sites More sharing options...
DaveyK Posted March 21, 2013 Author Share Posted March 21, 2013 I already edited the function because it didn't act the way I wanted to. I don't really follow your logic, but I'll post the new version here tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1420218 Share on other sites More sharing options...
DaveyK Posted March 22, 2013 Author Share Posted March 22, 2013 Requinix, this is the function I am currently using: function find_parent_id_0 ($k, $comments) { if (!isset($comments[$k])) { return false; } $parent = $comments[$k]['parent_id']; if ($parent == 0) { return $comments[$k]['comment_id']; } else { $id = find_parent_id_0 (($k - 1), $comments); } return $id; } Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1420281 Share on other sites More sharing options...
DavidAM Posted March 22, 2013 Share Posted March 22, 2013 What is it that you are actually looking for? I think requinix's answer assumes you want the array index of the parent comment, which is the logical assumption. However, your function is simply walking up the list of comments looking for someone with no parent. Depending on how the data is generated, this may or may not be the parent of the comment you started with. In addition, you stated "I only allow one level of replies, so no reply to a reply" -- however, the comment at index 4 has a parent_id of 2; comment_id 2 is at array index 1 and has a parent_id of 1; which seems to me to say that 4 is a reply to 2, which is a reply to 1. If you are trying to find the parent of a specific comment, the solution is different from trying to find the previous top-level comment. I think I might load the array differently, but again, that depends on what you need to accomplish from this dataset. Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1420341 Share on other sites More sharing options...
DaveyK Posted March 24, 2013 Author Share Posted March 24, 2013 (edited) You are right. My own data is kicking my butt. Well okay then, here's an example... Keep in mind that the data below is a comment section in a blog. The easiest method is obviously comments with 0 levels of replies Post1 Post2 Post3 Post4 Ideally, I would like N levels of replies. However, it was decided to allow 1 level of replies. Much quicker and easier, so: Post1 .. Post 5 Post2 Post3 .. Post 6 .. Post 7 Post4 Now what you obviously need with replies is a reply link (?reply_to=ID), but I only wanna show them once for every 'post'. I don't know how to explain this, but here it goes. The reply link would show up: Post1 .. Post 5 <-- here Post2 <-- here Post3 .. Post 6 .. Post 7 <-- here Post4 <-- here Again, this is easy for post 2 and 4, but for post 1 and 3 I need to know the id, but th problem I was facing was that I was already at array key 5 (post 7) whereas I need the ID from array key 3 (post 3). I have it working without any known bugs, but I am open to suggestions... Thanks for the help and the useful discussion anyway. PS I realize that this could also be done in CSS but I accepted the challenge in PHP. The first data set is wrong because I didn't have it working yet with the reply ids then so I wasn't organized at all Edited March 24, 2013 by DaveyK Quote Link to comment https://forums.phpfreaks.com/topic/275900-php-search-an-array-value/#findComment-1420620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.