0

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

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 处于同一级别。知道如何实现这一目标吗?

4

1 回答 1

1

你去,代码做你想做的事。请记住,代码确实希望每个部分具有相同数量的值。因此,如果您有两个 id,您还需要在 content、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
                )

        )

)
于 2013-01-08T08:36:13.107 回答