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

How do I get the first value tuesday of key week-day and combine all events_ids of all tuesdays?

closed as off-topic by Jacob Peattie, Tom J Nowell Nov 28 '17 at 1:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • This kind of question would be best asked on stack overflow, as it isn't specific to WordPress – Tom J Nowell Nov 28 '17 at 1:33
1

This should get the desired result:

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

side note: you may want to consider changing the array keys to either be all - based or all _ based for sanity.

0

This will copy first unique weekday to the new array and then if that weekday repeats it will just add the events IDs.

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

If the events in the initial array are not events just from one week you should focus on the day-date - associate all events IDs with it and then you can easily get the weekday name for that unique date. That approach would be much more versatile.

Not the answer you're looking for? Browse other questions tagged or ask your own question.