Question

I've been searching but could not find a working solution for my problem in PHP.

I've got an array with comments on which users can give comments. You can see that every comment has a value named "level". I'd like to have a flat array of 8 objects in the same order as they show now. So that would be "comment_id" -> 1,2,3,8,4,5,6,7.

EDIT: So this is what I'm using to build the array:

function loopThroughComments($rows = null, $row = null, $flat = false, $level){
    $limit = count($rows);
    for($i = 0; $i < $limit; $i++){
        $rows[$i]['comments'] = loopThroughComments($rows[$i]['comments'], $row, $flat, $level++);
        if($rows[$i]['comment_id'] == $row['comment_on_id']){
            if($flat){
                $row['level'] = $level;
            }
            array_push($rows[$i]['comments'], $row);
            break;
        }
    }
    return $rows;
}

Now, when the $flat == true, I'd like to make the array flat, but whatever I try, the format gets messed up

So I've got this array:

 Array
    (
        [0] => Array
            (
                [comment_id] => 1
                [user_id] => 3
                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                [comment_on_id] => 0
                [comment_text] => Dit is een eerste comment
                [comment_likes] => 6
                [comments] => Array
                    (
                        [0] => Array
                            (
                                [comment_id] => 2
                                [user_id] => 4
                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                [comment_on_id] => 1
                                [comment_text] => Dit is een reactie op dit ding hiero
                                [comment_likes] => 1
                                [comments] => Array
                                    (
                                        [0] => Array
                                            (
                                                [comment_id] => 3
                                                [user_id] => 3
                                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                                [comment_on_id] => 2
                                                [comment_text] => test 123
                                                [comment_likes] => 7
                                                [comments] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                                [comment_id] => 8
                                                                [user_id] => 4
                                                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                                                [comment_on_id] => 3
                                                                [comment_text] => level 2 comment
                                                                [comment_likes] => 7
                                                                [comments] => Array
                                                                    (
                                                                    )

                                                                [level] => 3
                                                            )

                                                    )

                                                [level] => 2
                                            )

                                        [1] => Array
                                            (
                                                [comment_id] => 4
                                                [user_id] => 3
                                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                                [comment_on_id] => 2
                                                [comment_text] => test 9999
                                                [comment_likes] => 0
                                                [comments] => Array
                                                    (
                                                    )

                                                [level] => 2
                                            )

                                        [2] => Array
                                            (
                                                [comment_id] => 5
                                                [user_id] => 3
                                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                                [comment_on_id] => 2
                                                [comment_text] => test 0000
                                                [comment_likes] => 0
                                                [comments] => Array
                                                    (
                                                    )

                                                [level] => 2
                                            )

                                    )

                                [level] => 1
                            )

                        [1] => Array
                            (
                                [comment_id] => 6
                                [user_id] => 3
                                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                                [comment_on_id] => 1
                                [comment_text] => test 555
                                [comment_likes] => 1
                                [comments] => Array
                                    (
                                    )

                                [level] => 1
                            )

                    )

                [level] => 0
            )

        [1] => Array
            (
                [comment_id] => 7
                [user_id] => 3
                [article_id] => 1345da9bae3e3bb436626422006abe04748432b9
                [comment_on_id] => 0
                [comment_text] => dit is een tweede comment
                [comment_likes] => 1
                [comments] => Array
                    (
                    )

                [level] => 0
            )

    )
Était-ce utile?

La solution

This code should work:

<?php

$result = array();

$arr = $arr[0];

while (is_array($arr['comments']) {
  $el = $arr;
  unset($el['comments']);
  $result[] = $el; 
  if (isset($arr['comments'])) {
    $arr = $arr['comments'];
  }
  else {
    break;
 }
}

You haven't provided data in PHP so I couldn't test it. However this code is definitely not optimal.

If you sure about fields you have in your array I strongly recommend to change lines:

  $el = $arr;
  unset($el['comments']);
  $result[] = $el; 

into

  $el['comment_id'] = $arr['comment_id'];
  $el['user_id'] = $arr['user_id'];
  // and so on until
  $el['comment_likes'] = $arr['comment_likes'];

Another solution will be creating extra loop but this solution should be enough in your case.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top