简体   繁体   中英

Create array from an array in the same array in PHP

I have an array which looks like this:

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
    )

)

And I want to restructure it to look like this:

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
            )
    )

)

Is this possible?

I have tried like(updated version, working):

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

But the inner_array I pushed goes higher level than [LogDate]. They should be on same level sa LogDate. Any idea how to achieve this?

There you go, the code to do what you want. Keep in mind that the code does expect an equal number of values for each part. So if you have two id's, you also need to values in the content, authers and times part. Also the content can not have a comma in it, besides from the seperator.

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

Output:

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
                )

        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM