How to combine array info

时间:2017-11-27 作者:techtransferportal

Array ( 
    [0] => Array ( [week-day] => tuesday [day-date] => 1511841600 [events_ids] => Array ( [0] => 83417 ) ) 
    [1] => Array ( [week-day] => wednesday [day-date] => 1511913600 [events_ids] => Array ( [0] => 83417 ) ) 
    [2] => Array ( [week-day] => thrusday [day-date] => 1512000000 [events_ids] => Array ( [0] => 83417 ) ) 
    [3] => Array ( [week-day] => tuesday [day-date] => 1511830800 [events_ids] => Array ( [0] => 83411 ) ) 
)
如何获取第一个值tuesday 共键week-day 并合并所有events_ids 所有的星期二?

2 个回复
最合适的回答,由SO网友:Andrew Herder 整理而成

这应该会得到期望的结果:

//Initial Array
$events = array
(
    array
    (
        \'week-day\' => \'tuesday\',
        \'day-date\' => 1511841600,
        \'events_ids\' => array( 83417 )
    ),
    array
    (
        \'week-day\' => \'wednesday\',
        \'day-date\' => 1511913600,
        \'events_ids\' => array( 83419, 12345 )
    ),
    array
    (
        \'week-day\' => \'tuesday\',
        \'day-date\' => 1511830800,
        \'events_ids\' => array( 83411 )
    )
);

//Array to fill
$tuesday_event_ids = array();

foreach($events as $event ):

    //if day is tuesday & we have event IDs
    if($event[\'week-day\'] === \'tuesday\' && count($event[\'events_ids\'])):
        foreach($event[\'events_ids\'] as $id):

            //Push to array if not already in it
            if(!in_array($id,$tuesday_event_ids))
                array_push($tuesday_event_ids, $id);

        endforeach;
    endif;

endforeach;

//should be: array(83417,83411)
var_dump($tuesday_event_ids);
旁注:您可能需要考虑将数组键更改为全部或全部- 基于或全部_ 基于理智。

SO网友:Greg36

这会将第一个唯一的工作日复制到新数组中,如果该工作日重复,则只会添加事件ID。

$events = [
    [
        \'week-day\' => \'tuesday\',
        \'day-date\' => 1511841600,
        \'events_ids\' => [83417]
    ],
    [
        \'week-day\' => \'wednesday\',
        \'day-date\' => 1511913600,
        \'events_ids\' => [83417]
    ],
    [
        \'week-day\' => \'thrusday\',
        \'day-date\' => 1512000000,
        \'events_ids\' => [83417]
    ],
    [
        \'week-day\' => \'tuesday\',
        \'day-date\' => 1511830800,
        \'events_ids\' => [83417]
    ]
];

$new_events = [];

foreach ( $events as $event ) {
    if ( array_key_exists( $event[\'week-day\'], $new_events ) ) {
        // If we have this week day just merge the event ids.
        $new_events[$event[\'week-day\']][\'events_ids\'] = array_merge(
            $new_events[$event[\'week-day\']][\'events_ids\'],
            $event[\'events_ids\']
        );
    } else {
        $new_events[$event[\'week-day\']] = $event;
    }
}

$new_events = array_values($new_events);
如果初始数组中的事件不是仅来自一周的事件,则应关注day-date - 将所有事件ID与之关联,然后您可以轻松获得该唯一日期的工作日名称。这种方法将更加通用。

结束

相关推荐

如何使用phpMyAdmin替换WordPress数据库中的值

我试图使用phpmyadmin在cg\\U选项行/字段中保存一个值,但数据存储不正确,保存后再次加载时,只显示部分数据。我是否需要在phpmyadmin中使用转换函数?该行为theme\\u mods\\u[主题名称]这是字段中包含的数据示例:s:19:\"thim_my_text_option\" s:0:\"\" s:30:\"thim_display_login_menu_mobile\" b:0 s:25:\"thim_body_secondary_color\"&