[ create a new paste ] login | about

Link: http://codepad.org/5hJVItQb    [ raw code | output | fork ]

PHP, pasted on Mar 4:
<?PHP
/* 
* 新しい配列 $new
*/
$new = array(
    '2019-01-02'=> array(
        array(
            'action'=>'banana',
            'time'=>'01-02',      
            'targetID'=>'1', 
            'actorID'=>'17', 
            'subID'=>'77', 
        )
    )
);

/* 
* 現在の配列 $current
*/
$current = array(
    '2019-01-02'=> array(
        array(
            'action'=>'orange',
            'time'=>array('01-02','01-01'),    
            'targetID'=>'200',         
            'actorID'=>array('16','15'),
            'subID'=>array('6','22'),
        ), 
        array(
            'action'=>'apple',
            'time'=>array('01-02','01-01'), 
            'targetID'=>'1',         
            'actorID'=>array('14','13'),
            'subID'=>array('86','64'),
        ),
    ),   
    '2019-01-01'=> array(        
        array(
            'action'=>'apple',
            'time'=>array('01-01','01-01'),  
            'targetID'=>'1',          
            'actorID'=>array('4','3'),   
            'subID'=>array('1','33'),
        ),    
    ),    
);

foreach($new as $day => $contents) {
    foreach($contents as $content) {
        //【処理1】同じ年月日がなければ、年月日の配列を連結して終了
        if(!isset($current[$day])) {
            $current = array_merge($new, $current);
            continue;
        } 
        //【処理2】同じactionがなければ、年月日の配列を連結して終了
        if($current[$day]['action'] !== $content['action']) {
            $current = array_merge($new, $current);
            continue;
        }         
        //【処理3】同じ年月日で同じactionがあれば、年月日の配列の中の3つの配列を連結して、新しい20個だけをとっておく
        for($i = 0; $i < count($current[$day]); $i++) {
            $current[$day][$i]['time'] = array_slice(array_merge([$content['time']], $current[$day][$i]['time']), 0, 20);
            $current[$day][$i]['actorID'] = array_slice(array_merge([$content['actorID']], $current[$day][$i]['actorID']), 0, 20);
            $current[$day][$i]['subID'] = array_slice(array_merge([$content['subID']], $current[$day][$i]['subID']), 0, 20);
        }
        //新しいのを上に
        $sortkey = array_map(function($ar) { return $ar[0];}, array_column($current[$day], 'time'));
        array_multisort($sortkey, SORT_DESC,$current[$day]);
    }
}
var_export($current);


Output:
1
2

Parse error: syntax error, unexpected '[', expecting ')' on line 62


Create a new paste based on this one


Comments: