多维数组到平面数组php

时间:2014-05-10 16:35:31

标签: php arrays multidimensional-array

我一直在寻找,但在PHP中找不到解决我的问题的方法。

我有一个数组,其中包含哪些用户可以发表评论的评论。您可以看到每条评论都有一个名为“level”的值。我想要一个8个对象的平面数组,其顺序与现在显示的顺序相同。那就是“comment_id” - > 1,2,3,8,4,5,6,7。

编辑: 所以这就是我用来构建数组的东西:

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

现在,当$ flat == true时,我想让数组变平,但无论我尝试什么,格式都搞砸了

所以我有这个阵列:

 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
            )

    )

1 个答案:

答案 0 :(得分:0)

此代码应该有效:

<?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;
 }
}

你没有用PHP提供数据,所以我无法测试它。 但是这段代码绝对不是最佳的。

如果您确定数组中的字段,我强烈建议您更改行:

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

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

另一种解决方案是创建额外的循环,但在您的情况下,此解决方案应该足够了。