Guest User

Untitled

a guest
Jan 12th, 2019
2,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. Parsing through a giant mulltidimensional array and construct a new array from it
  2. Array
  3. (
  4. [0] => Array
  5. (
  6. [id] => 86
  7. [34] => 695
  8. [39] => 0
  9. [40.1] => Yes
  10. [36.1] => Yes
  11. [35.4] => Card
  12. [33.3] => Dekalb
  13. [33.4] => Illinois
  14. [33.5] => 60115
  15. [33.6] => United States
  16. [35.1] => 1143
  17. [33.1] => 5555 Write Rd
  18. [33.2] => Write School
  19. [32.6] => John
  20. [32.3] => Smith
  21. [28] => jsmith@gmail.com
  22. [27] => 5555556554
  23. [25] => NIUSN
  24. [14.3] => Jane
  25. [14.6] => Doe
  26. [11.2] => 695
  27. [12] => 1
  28. [11.1] => In-Person
  29. [3] => 0
  30. [2.2] => 595
  31. [2.1] => Online
  32. )
  33. [1] => Array
  34. (
  35. ...same stuff as before
  36. )
  37.  
  38. function parseArray($arry) {
  39.  
  40. $results = array();
  41. $current_result = array();
  42.  
  43. foreach($arry as $a) {
  44. foreach($a as $k => $v) {
  45. if ( $k == '14.3' ) {
  46. $attendee_first_name = $v;
  47. }
  48. if ( $k == '14.6' ) {
  49. $attendee_last_name = $v;
  50. }
  51. if ( $attendee_first_name && $attendee_last_name ) {
  52. $full_name = $attendee_first_name . ' ' . $attendee_last_name;
  53. }
  54. }
  55. $current_result['attendee_name'] = $full_name;
  56. }
  57.  
  58. array_push($results, $current_result);
  59.  
  60. return $results;
  61. }
  62.  
  63. Array
  64. (
  65. [0] => Array
  66. (
  67. [attendees_name] = John Smith
  68. [attendees_email] = jsmith@gmail.com
  69. [purchasing_name] = Jane Doe
  70. ...etc
  71.  
  72. # initialize your test array
  73.  
  74. $arry = array(
  75. 0 => array(
  76. 'id' => 86,
  77. '34' => 695,
  78. '39' => 0,
  79. '40.1' => 'Yes',
  80. '36.1' => 'Yes',
  81. '35.4' => 'Card',
  82. '33.3' => 'Dekalb',
  83. '33.4' => 'Illinois',
  84. '33.5' => '60115',
  85. '33.6' => 'United States',
  86. '35.1' => '1143',
  87. '33.1' => '5555 Write Rd',
  88. '33.2' => 'Write School',
  89. '32.6' => 'John',
  90. '32.3' => 'Smith',
  91. '28' => 'jsmith@gmail.com',
  92. '27' => '5555556554',
  93. '25' => 'NIUSN',
  94. '14.3' => 'Jane',
  95. '14.6' => 'Doe',
  96. '11.2' => '695',
  97. '12' => '1',
  98. '11.1' => 'In-Person',
  99. '3' => 0,
  100. '2.2' => 595,
  101. '2.1' => 'Online'
  102. ),
  103. 1 => array(
  104. 'id' => 86,
  105. '34' => 695,
  106. '39' => 0,
  107. '40.1' => 'Yes',
  108. '36.1' => 'Yes',
  109. '35.4' => 'Card',
  110. '33.3' => 'Dekalb',
  111. '33.4' => 'Illinois',
  112. '33.5' => '60115',
  113. '33.6' => 'United States',
  114. '35.1' => '1143',
  115. '33.1' => '5555 Write Rd',
  116. '33.2' => 'Write School',
  117. '32.6' => 'Douglas',
  118. '32.3' => 'Adams',
  119. '28' => 'douglas.adams@gmail.com',
  120. '27' => '5555556554',
  121. '25' => 'NIUSN',
  122. '14.3' => 'Frank',
  123. '14.6' => 'Wright',
  124. '11.2' => '695',
  125. '12' => '1',
  126. '11.1' => 'In-Person',
  127. '3' => 0,
  128. '2.2' => 595,
  129. '2.1' => 'Online'
  130. )
  131.  
  132. );
  133.  
  134. # How to do your function elegantly
  135.  
  136. foreach($arry as $a) {
  137. $results[] = array(
  138. 'attendees_name' => $a['32.6'] . " " . $a['32.3'],
  139. 'attendees_email' => $a['28'],
  140. 'purchasing_name' => $a['14.3'] . " " . $a['14.6']
  141. );
  142. }
  143.  
  144. # print results
  145.  
  146. var_dump($results);
  147.  
  148. array(2) {
  149. [0]=>
  150. array(3) {
  151. ["attendees_name"]=>
  152. string(10) "John Smith"
  153. ["attendees_email"]=>
  154. string(16) "jsmith@gmail.com"
  155. ["purchasing_name"]=>
  156. string(8) "Jane Doe"
  157. }
  158. [1]=>
  159. array(3) {
  160. ["attendees_name"]=>
  161. string(13) "Douglas Adams"
  162. ["attendees_email"]=>
  163. string(23) "douglas.adams@gmail.com"
  164. ["purchasing_name"]=>
  165. string(12) "Frank Wright"
  166. }
  167. }
Add Comment
Please, Sign In to add comment