从PHP中的同一数组中的数组创建数组

时间:2013-01-08 08:15:16

标签: php arrays

我有一个看起来像这样的数组:

Array
(
[0] => Array
    (
        [LogDate] => Wed , Nov 14 , 2012
        [LogIDs] => 1,2
        [LogContents] => Hello!,Hello 2...
        [LogAuthors] => yan,yan
        [LogTimes] => 12:00 AM,12:00 AM
    )

[1] => Array
    (
        [LogDate] => Thu , Nov 15 , 2012
        [LogIDs] => 3,4
        [LogContents] => Hello world...,Hi world...
        [LogAuthors] => jonathan,jonathan
        [LogTimes] => 12:00 AM,12:00 AM
    )

我想重组它看起来像这样:

Array
(
[0] => Array
    (
        [LogDate] => Wed , Nov 14 , 2012
        [0] => Array
            (
                [LogIDs] => 1
                [LogContents] => Hello!
                [LogAuthors] => yan
                [LogTimes] => 12:00 AM
            )
        [1] => Array
            (
                [LogIDs] => 2
                [LogContents] => Hello 2...
                [LogAuthors] => yan
                [LogTimes] => 12:00 AM
            )
    )
[1] => Array
    (
        [LogDate] => Thu , Nov 15 , 2012
        [0] => Array
            (
                [LogIDs] => 3
                [LogContents] => Hello world...
                [LogAuthors] => jonathan,jonathan
                [LogTimes] => 12:00 AM
            )
        [1] => Array
            (
                [LogIDs] => 4
                [LogContents] => Hi world...
                [LogAuthors] => jonathan
                [LogTimes] => 12:00 AM
            )
    )

这可能吗?

我试过(更新版本,工作):

$result =   $this->mymodel->fetch_logs(1, 5, 0);
    $new_array = array();
    $inner_array = array();
    $kounter = 0;
    foreach($result as $row){

        $new_array[] = array(
        'LogDate' => $row['LogDate'],           
        );

        $log_ids = explode(',',$row['LogIDs']);
        $log_contents = explode(',',$row['LogContents']);
        $log_authors = explode(',',$row['LogAuthors']);
        $log_times = explode(',',$row['LogTimes']);

        $counter = count($log_ids);

        for($i=0; $i<$counter; $i++){
            $inner_array = array(
                'LogID' => $log_ids[$i],
                'LogContent' => $log_contents[$i],
                'LogAuthor' => $log_authors[$i],
                'LogTime' => $log_times[$i]
            );                              
            //array_push($new_array, $inner_array); 
            $new_array[$kounter][$i] = $inner_array;                                    
        }   
        $kounter++;
    }

但是我推动的inner_array比[LogDate]更高。它们应该在同一级别的LogDate上。知道如何实现这个目标吗?

1 个答案:

答案 0 :(得分:1)

你去了,代码做你想要的。请记住,代码确实期望每个部分具有相同数量的值。因此,如果你有两个id,你还需要在内容,authers和times部分中赋值。除了分隔符之外,内容中也没有逗号。

<pre>
<?php
$ar[0] = ['LogDate'=>'Wed , Nov 14 , 2012', 'LogIDs' => '1,2', 'LogContents' => 'Hello!,Hello 2...', 'LogAuthors' => 'yan,yan', 'LogTimes' => '12:00 AM,12:00 AM'];
$ar[1] = ['LogDate'=>'Thu , Nov 15 , 2012', 'LogIDs' => '3,4', 'LogContents' => 'Hello world...,Hi world...', 'LogAuthors' => 'jonathan,jonathan', 'LogTimes' => '12:00 AM,12:00 AM'];
print_r($ar);

$newar = [];
foreach($ar as $key=>$val) {
    $newar[$key] = [];
    $date = $val['LogDate'];
    $arIds = explode(',', $val['LogIDs']);
    $arContents = explode(',', $val['LogContents']);
    $arAuthors = explode(',', $val['LogAuthors']);
    $arTimes = explode(',', $val['LogTimes']);

    $newar[$key]['LogDate'] = $date;
    foreach($arIds as $k=>$v) {
      $tar = [];
      $tar['LogIDs'] = $v;
      $tar['LogContents'] = $arContents[$k];
      $tar['LogAuthors'] = $arAuthors[$k];
      $tar['LogTimes'] = $arTimes[$k];

      $newar[$key][] = $tar;
    }
}

print_r($newar);


?>
</pre>

输出:

Array
(
    [0] => Array
        (
            [LogDate] => Wed , Nov 14 , 2012
            [LogIDs] => 1,2
            [LogContents] => Hello!,Hello 2...
            [LogAuthors] => yan,yan
            [LogTimes] => 12:00 AM,12:00 AM
        )

    [1] => Array
        (
            [LogDate] => Thu , Nov 15 , 2012
            [LogIDs] => 3,4
            [LogContents] => Hello world...,Hi world...
            [LogAuthors] => jonathan,jonathan
            [LogTimes] => 12:00 AM,12:00 AM
        )
)
Array
(
    [0] => Array
        (
            [LogDate] => Wed , Nov 14 , 2012
            [0] => Array
                (
                    [LogIDs] => 1
                    [LogContents] => Hello!
                    [LogAuthors] => yan
                    [LogTimes] => 12:00 AM
                )

            [1] => Array
                (
                    [LogIDs] => 2
                    [LogContents] => Hello 2...
                    [LogAuthors] => yan
                    [LogTimes] => 12:00 AM
                )

        )

    [1] => Array
        (
            [LogDate] => Thu , Nov 15 , 2012
            [0] => Array
                (
                    [LogIDs] => 3
                    [LogContents] => Hello world...
                    [LogAuthors] => jonathan
                    [LogTimes] => 12:00 AM
                )

            [1] => Array
                (
                    [LogIDs] => 4
                    [LogContents] => Hi world...
                    [LogAuthors] => jonathan
                    [LogTimes] => 12:00 AM
                )

        )

)