Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing through a giant mulltidimensional array and construct a new array from it
    text
    copied!<p>I need some help starting a function that will parse through an array, check for certain values, if those values exist, create a new array with those values and array_push them all together.</p> <p>I'm passing through an array such as this:</p> <pre><code>Array ( [0] =&gt; Array ( [id] =&gt; 86 [34] =&gt; 695 [39] =&gt; 0 [40.1] =&gt; Yes [36.1] =&gt; Yes [35.4] =&gt; Card [33.3] =&gt; Dekalb [33.4] =&gt; Illinois [33.5] =&gt; 60115 [33.6] =&gt; United States [35.1] =&gt; 1143 [33.1] =&gt; 5555 Write Rd [33.2] =&gt; Write School [32.6] =&gt; John [32.3] =&gt; Smith [28] =&gt; jsmith@gmail.com [27] =&gt; 5555556554 [25] =&gt; NIUSN [14.3] =&gt; Jane [14.6] =&gt; Doe [11.2] =&gt; 695 [12] =&gt; 1 [11.1] =&gt; In-Person [3] =&gt; 0 [2.2] =&gt; 595 [2.1] =&gt; Online ) [1] =&gt; Array ( ...same stuff as before ) </code></pre> <p>I made a function <code>parseArray</code> to which I passed the above array. I go through every key/value combo and if a certain key exists, I set that to a variable. Then if all the right variables exist, I associate that to an array then push it to a final array (where all values will be held):</p> <pre><code>function parseArray($arry) { $results = array(); $current_result = array(); foreach($arry as $a) { foreach($a as $k =&gt; $v) { if ( $k == '14.3' ) { $attendee_first_name = $v; } if ( $k == '14.6' ) { $attendee_last_name = $v; } if ( $attendee_first_name &amp;&amp; $attendee_last_name ) { $full_name = $attendee_first_name . ' ' . $attendee_last_name; } } $current_result['attendee_name'] = $full_name; } array_push($results, $current_result); return $results; } </code></pre> <p>Now they way I have been doing it, has been very procedural and very clunky. I would love to get some insight on how to produce much cleaner/beautiful code for traversing an array and assigning value.</p> <p>Ideally I would love something like this to result:</p> <pre><code>Array ( [0] =&gt; Array ( [attendees_name] = John Smith [attendees_email] = jsmith@gmail.com [purchasing_name] = Jane Doe ...etc </code></pre> <p>So I can simply pass the output through a foreach and output the desired information easily.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload