Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Parsing through a giant mulltidimensional array and construct a new array from it
- Array
- (
- [0] => Array
- (
- [id] => 86
- [34] => 695
- [39] => 0
- [40.1] => Yes
- [36.1] => Yes
- [35.4] => Card
- [33.3] => Dekalb
- [33.4] => Illinois
- [33.5] => 60115
- [33.6] => United States
- [35.1] => 1143
- [33.1] => 5555 Write Rd
- [33.2] => Write School
- [32.6] => John
- [32.3] => Smith
- [28] => jsmith@gmail.com
- [27] => 5555556554
- [25] => NIUSN
- [14.3] => Jane
- [14.6] => Doe
- [11.2] => 695
- [12] => 1
- [11.1] => In-Person
- [3] => 0
- [2.2] => 595
- [2.1] => Online
- )
- [1] => Array
- (
- ...same stuff as before
- )
- function parseArray($arry) {
- $results = array();
- $current_result = array();
- foreach($arry as $a) {
- foreach($a as $k => $v) {
- if ( $k == '14.3' ) {
- $attendee_first_name = $v;
- }
- if ( $k == '14.6' ) {
- $attendee_last_name = $v;
- }
- if ( $attendee_first_name && $attendee_last_name ) {
- $full_name = $attendee_first_name . ' ' . $attendee_last_name;
- }
- }
- $current_result['attendee_name'] = $full_name;
- }
- array_push($results, $current_result);
- return $results;
- }
- Array
- (
- [0] => Array
- (
- [attendees_name] = John Smith
- [attendees_email] = jsmith@gmail.com
- [purchasing_name] = Jane Doe
- ...etc
- # initialize your test array
- $arry = array(
- 0 => array(
- 'id' => 86,
- '34' => 695,
- '39' => 0,
- '40.1' => 'Yes',
- '36.1' => 'Yes',
- '35.4' => 'Card',
- '33.3' => 'Dekalb',
- '33.4' => 'Illinois',
- '33.5' => '60115',
- '33.6' => 'United States',
- '35.1' => '1143',
- '33.1' => '5555 Write Rd',
- '33.2' => 'Write School',
- '32.6' => 'John',
- '32.3' => 'Smith',
- '28' => 'jsmith@gmail.com',
- '27' => '5555556554',
- '25' => 'NIUSN',
- '14.3' => 'Jane',
- '14.6' => 'Doe',
- '11.2' => '695',
- '12' => '1',
- '11.1' => 'In-Person',
- '3' => 0,
- '2.2' => 595,
- '2.1' => 'Online'
- ),
- 1 => array(
- 'id' => 86,
- '34' => 695,
- '39' => 0,
- '40.1' => 'Yes',
- '36.1' => 'Yes',
- '35.4' => 'Card',
- '33.3' => 'Dekalb',
- '33.4' => 'Illinois',
- '33.5' => '60115',
- '33.6' => 'United States',
- '35.1' => '1143',
- '33.1' => '5555 Write Rd',
- '33.2' => 'Write School',
- '32.6' => 'Douglas',
- '32.3' => 'Adams',
- '28' => 'douglas.adams@gmail.com',
- '27' => '5555556554',
- '25' => 'NIUSN',
- '14.3' => 'Frank',
- '14.6' => 'Wright',
- '11.2' => '695',
- '12' => '1',
- '11.1' => 'In-Person',
- '3' => 0,
- '2.2' => 595,
- '2.1' => 'Online'
- )
- );
- # How to do your function elegantly
- foreach($arry as $a) {
- $results[] = array(
- 'attendees_name' => $a['32.6'] . " " . $a['32.3'],
- 'attendees_email' => $a['28'],
- 'purchasing_name' => $a['14.3'] . " " . $a['14.6']
- );
- }
- # print results
- var_dump($results);
- array(2) {
- [0]=>
- array(3) {
- ["attendees_name"]=>
- string(10) "John Smith"
- ["attendees_email"]=>
- string(16) "jsmith@gmail.com"
- ["purchasing_name"]=>
- string(8) "Jane Doe"
- }
- [1]=>
- array(3) {
- ["attendees_name"]=>
- string(13) "Douglas Adams"
- ["attendees_email"]=>
- string(23) "douglas.adams@gmail.com"
- ["purchasing_name"]=>
- string(12) "Frank Wright"
- }
- }
Add Comment
Please, Sign In to add comment