连接数组以进入多维数组

时间:2015-08-23 14:26:30

标签: php mysql arrays multidimensional-array merge

我需要动态生成数组的子元素,这些是自己的数组。所以最终结果应该是(这是下面代码的简化版本,以便更容易理解):

Array
(
    [id] => 5000038642
    [name] => TrackVia Legacy Section of Array
    [description] => 
    [table_id] => 5000005024
    [records] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [table_id] => 1
                    [fields] => Array
                        (
                            [Name] => First Item
                        )
                   )
              [1] => Array
                (
                    [id] => 1
                    [table_id] => 1
                    [fields] => Array
                        (
                            [Name] => Second Item
                        )
                   )
        )
)

结果中的嵌套数组是从MySQL表动态生成的,所以>

$allItems = array();
    $item = array();

// Get each result and build the arrya
while($row = $resultAvailableBoxes->fetch_assoc()) {

                $item = array (
                "id"=> $row['id'], 
                "table_id"=> $row['id'], 
                "fields"=> array (
                    "Name"=> $row['box_title'], 
                    )
                );

                // Append the arrays on to each other
                $allItems[] = array_merge($allItems, $item);

    }

// Place the arrays within the "parent" array
$completeArray = array( 
            "id"=> 1000, 
            "name"=> "Sample",
            "description"=> "", 
            "table_id"=> 1000, 
                "records"=> 
                    $allItems

                );

正如您所看到的那样,我还尝试将每个新数组附加到最后一个,然后将这些数组放入" parent"阵列。这就是问题发生的地方。

使用方法

  

$ allItems [] = array_merge($ allItems,$ item);

我将每个数组附加到最后一个,但后来又一次又一次。像这样:

Array
(
    [id] => 5000038642
    [name] => TrackVia Legacy Section of Array
    [description] => 
    [table_id] => 5000005024
    [records] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [table_id] => 1
                    [fields] => Array
                        (
                            [Name] => Texan BBQ 1
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [table_id] => 1
                            [fields] => Array
                                (
                                    [Name] => Texan BBQ 1
                                )

                        )

                    [id] => 9
                    [table_id] => 9
                    [fields] => Array
                        (
                            [Name] => Goan Sorpotel with Pea & Mint Pilau and Tomato Chutney
                        )

                )

        )

)

当我有20个项目时,您会发现这将成为大量列表并且无法正常工作。使用方法

  

$ allItems = array_merge($ allItems,$ item);

仅返回附加的最后一个数组(即它总是覆盖" allItems"数组中已有的数据。

我还使用了一种我不希望工作的简单方法,而且确实没有:

  

$ allItems。= $ item;

我在阅读了这些看似同样但又没有给出奇怪结果的堆叠流问题之后得出结论,我必须接近这一切。 这是一个完全停止的错误方法,还是我错过了一些东西来阻止不断添加子元素?

Appending Arrays (Stack Overflow) Can't concatenate 2 arrays in PHP

我已经忘记了其他问题,包括PHP手册,但我找不到与array_merge相关的更多信息

2 个答案:

答案 0 :(得分:1)

尝试使用$allItems[] = $item;代替$allItems[] = array_merge($allItems, $item);

答案 1 :(得分:1)

为什么不这样做?

$items = array();


while($row = $resultAvailableBoxes->fetch_assoc()) {
    $items[] = array (
        "id"=> $row['id'], 
        "table_id"=> $row['id'], 
        "fields"=> array (
            "Name"=> $row['box_title'], 
            )
        );
}

// Place the arrays within the "parent" array
$completeArray = array( 
                "id"=> 1000, 
                "name"=> "Sample",
                "description"=> "", 
                "table_id"=> 1000, 
                "records"=> $items                    
                );

这应该创建一个包含所有项目的数组,然后将其添加到$completeArray