Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Multidimensional array to CSV
    text
    copied!<p>I currently have coded a way to turn a multidimensional array to comma separated values (I'm using pipes instead of commas for ease of debugging). The problem is, I know that the code I use to do this is bloody awful. It works how I want it to, but it's not nice at all.</p> <p><strong>What I need</strong></p> <p>Currently the <code>arr_to_csv()</code> function works for five levels of nested data within the multidimensional array. I need a recursive function to perform the same for one or an unlimited number of nested arrays, or a good nudge in the right direction. Recursion is <em>not</em> my strong point at all, but I know it's the way forward.</p> <p><strong>Data input</strong></p> <p>A multi-dimensional array is passed to the function.</p> <pre><code>array 'name' =&gt; array 'singular' =&gt; null 'plural' =&gt; null 'fields' =&gt; array 'price' =&gt; array 'label' =&gt; string 'Preis' (length=5) 'company_id' =&gt; array 'label' =&gt; null 'placeholder' =&gt; null //...the array could go on... </code></pre> <p><strong>The function returns the following...</strong></p> <p>This is exactly what I want...</p> <pre><code>0 =&gt; string 'name||singular||null' (length=20) 1 =&gt; string 'name||plural||null' (length=18) 2 =&gt; string 'fields||price||label||Preis' (length=27) 3 =&gt; string 'fields||company_id||label||null' (length=31) 4 =&gt; string 'fields||company_id||placeholder||null' (length=37) 5 =&gt; string 'fields||name||label||null' (length=25) 6 =&gt; string 'fields||name||placeholder||null' (length=31) </code></pre> <p><strong>My horrible constructed function</strong> </p> <p>I'm no good with recursion, so here's my awful list of <code>foreach</code>s. As you can see from the below code, this is terrible (no need to read the whole thing, it just copies itself). Please help me sort out my horrible code!</p> <pre><code>function arr_to_csv($data,$csv = '||') { $array = array(); /* Epic amount of for each's. This could be done with recursion */ foreach($data as $key =&gt; &amp;$value) { if (!is_array($value)) { $array[] = $key . $csv .(is_null($value)?'null':$value); } else { foreach ($value as $k =&gt; &amp;$v) { if (!is_array($v)) { $array[] = $key . $csv . $k . $csv . (is_null($v) ? 'null' : $v); } else { foreach ($v as $kk =&gt; &amp;$vv) { if (!is_array($vv)) { $array[] = $key . $csv . $k . $csv . $kk . $csv . (is_null($vv) ? 'null' : $vv); } else { foreach ($vv as $x =&gt; &amp;$y) { if (!is_array($y)) { $array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . (is_null($y) ? 'null' : $y); } else { foreach ($y as $too =&gt; $long) { if(!is_array($long)) { $array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . $too . $csv. (is_null($long)?'null':$long); } else { foreach ($long as $omg =&gt; $why) { if(!is_array($why)) { $array[] = $key . $csv . $k . $csv . $kk . $csv. $x . $csv . $too . $csv . $omg . $csv . (is_null($why) ? 'null' : $why); } } } } } } } } } } } } return $array; } </code></pre>
 

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