php - How to iterate over all the inner array elements and make modification to the original multidimensional array? -
i'm relatively newbie php arrays , i've big challenging task in front of me, please understand situation.
i've multidimensional array titled $events
follows :
//output of print_r($events); array ( [0] => array ( [0] => array ( [rsvp_id] => 2 [event_id] => 237 [formatted_start_time] => 1443052800 [formatted_end_time] => 1443052800 [event_group_date] => 24 sep, thursday 2015 ) [1] => array ( [rsvp_id] => [event_id] => 295 [formatted_start_time] => 1443070800 [formatted_end_time] => 1443070800 [event_group_date] => 24 sep, thursday 2015 ) [2] => array ( [rsvp_id] => [event_id] => 294 [formatted_start_time] => 1443099600 [formatted_end_time] => 1443099600 [event_group_date] => 24 sep, thursday 2015 ) [3] => array ( [rsvp_id] => [event_id] => 297 [formatted_start_time] => 1443101100 [formatted_end_time] => 1443101100 [event_group_date] => 24 sep, thursday 2015 ) [4] => array ( [rsvp_id] => [event_id] => 296 [formatted_start_time] => 1443103200 [formatted_end_time] => 1443103200 [event_group_date] => 24 sep, thursday 2015 ) [5] => array ( [rsvp_id] => 1 [event_id] => 330 [formatted_start_time] => 1443118800 [formatted_end_time] => 1443122400 [event_group_date] => 24 sep, thursday 2015 ) [6] => array ( [rsvp_id] => [event_id] => 298 [formatted_start_time] => 1443124800 [formatted_end_time] => 1443124800 [event_group_date] => 24 sep, thursday 2015 ) ) [1] => array ( [0] => array ( [rsvp_id] => 2 [event_id] => 332 [formatted_start_time] => 1443189600 [formatted_end_time] => 1443193200 [event_group_date] => 25 sep, friday 2015 ) ) [2] => array ( [0] => array ( [rsvp_id] => [event_id] => 239 [formatted_start_time] => 1443571200 [formatted_end_time] => 1443571200 [event_group_date] => 30 sep, wednesday 2015 ) ) )
now want iterate on each of events above array, pass respective event_id
in each iteration function, receive array output returned function, conditional checking , based on condition result either keep or remove particular event element in above array.
for example, in 1 iteration i've pass event_id
332 function follows :
$aeventinvites = getinvites(332); //for understanding purpose hard coded event_id passed, should passed dynamically during each iteration.
after executing above statement receive following result:
//output of print_r($aeventinvites); array ( [0] => 2 [1] => array ( [0] => array ( [invite_id] => 698 [event_id] => 332 [type_id] => 0 [rsvp_id] => 0 [user_id] => 970 [invited_user_id] => 970 ) [1] => array ( [invite_id] => 697 [event_id] => 332 [type_id] => 0 [rsvp_id] => 0 [user_id] => 244 [invited_user_id] => 244 ) ) )
note : each event_id
array $events
function getinvites()
should call.
now let's talk condition. before consider below statement :
$looged_in_user_id = 244;
the final array $events
want contain elements satisfying either of following conditions :
the elements array
$events
should kept contains value in fieldrsvp_id
(it should not blank)the elements array
$events
should kept event iteration output satisfies following condition:- the
invited_user_id
should same$logged_in_user_id
,event_id
should present in array$aeventinvites
.
- the
for example in case of iteration event_id = 332
, array $aeventinvites
contains both these things follows :
array ( [invite_id] => 697 [event_id] => 332 [type_id] => 0 [rsvp_id] => 0 [user_id] => 244 [invited_user_id] => 244 )
so, in such case nothing element array $events
of event_id = 332
.
in short want final $events
array in following form :
//final required output of print_r($events); array ( [0] => array ( [0] => array ( [rsvp_id] => 2 [event_id] => 237 [formatted_start_time] => 1443052800 [formatted_end_time] => 1443052800 [event_group_date] => 24 sep, thursday 2015 ) [1] => array ( [rsvp_id] => 1 [event_id] => 330 [formatted_start_time] => 1443118800 [formatted_end_time] => 1443122400 [event_group_date] => 24 sep, thursday 2015 ) ) [1] => array ( [0] => array ( [rsvp_id] => 2 [event_id] => 332 [formatted_start_time] => 1443189600 [formatted_end_time] => 1443193200 [event_group_date] => 25 sep, friday 2015 ) ) )
note : array index should modified starting 0 onwards. in above expected output.
someone please me in achieving task. please feel free ask of doubt have regarding required output. thanks.
i hope got conditions right, atleast same result expecting given informations:
function process(array $events, $logged_in_user_id) { $newevents = []; foreach ($events $i => $key) { if (is_array($key) && !isset($key["event_id"])) { foreach ($key $index => $event) { if (is_array($event) && isset($event["event_id"])) { $conditions = false; if (!isset($event["rsvp_id"])) { $aeventinvites = getinvites($event["event_id"]); // try match foreach ($aeventinvites $aeikey => $aeivalue) { if (is_array($aeivalue)) { foreach ($aeivalue $invites) { if (isset($invites["invited_user_id"]) && isset($invites["event_id"])) { if ( $invites["invited_user_id"] == $logged_in_user_id && $invites["event_id"] == $event["event_id"] ) { $conditions = true; } } } } } } else { $conditions = true; } if ($conditions) { $newevents[] = $event; } } } } } return $newevents; }
you make use of continue;
instead of bool $conditions
variable inside last loop. in general, if able modify output of getinvites($event_id)
recommend change resulting array assoc array (meaning instead of number indexes use string indexes "num_of_invitees" , "invitees") access these fields directly without iterating on result (with leads little more performance).
it's not beatiful solution atleast working one. have fun.
edit: forgot fiddle link: http://ideone.com/xpntrn
Comments
Post a Comment