title: Sorting multidimensional arrays - PHP
body: <p>I have a .csv upload form that takes the values from the file and formats them appropriately.  Or at least that is what I am trying to accomplish.</p>
  
  <p>What I am trying to do is to format the <code>6</code> index of every inside array with the name that is held within, example:</p>
  
  <pre><code>Array (
    [0] =&gt; Array (
      [0] =&gt; 2
      [1] =&gt; 230
      [2] =&gt; 12 Aug 2013 07:46 AM
      [3] =&gt; 816
      [4] =&gt; INB
      [5] =&gt; 1.38
      [6] =&gt; Leonard
    )
  )
  Array (
    [0] =&gt; Array (
      [0] =&gt; 2
      [1] =&gt; 230
      [2] =&gt; 12 Aug 2013 07:46 AM
      [3] =&gt; 816
      [4] =&gt; INB
      [5] =&gt; 1.38
      [6] =&gt; Leonardo
    ) 
    [1] =&gt; Array(
      [0] =&gt; 3
      [1] =&gt; 170
      [2] =&gt; 12 Aug 2013 07:47 AM
      [3] =&gt; 170
      [4] =&gt; OUT (EXTENSION)
      [5] =&gt; 1.08
      [6] =&gt; Greg
    )
  )
  </code></pre>
  
  <p>So this <code>.csv</code> file will contain thousands of these entries but I am trying to format it so that all of the <code>Leonardo's</code> will be clumped together rather than 1 here and there and than a 100 rows down in the <code>.csv</code> file that is sent out to download.  </p>
  
  <p>Firstly let me explain how the data is retrieved.  A <code>while loop</code> opens the file and iterates through the entire file.  Data is analysed and than checked, each array is stored into this:</p>
  
  <pre><code>$collectedData[$i] = $tmpfileop;
  $i++;
  </code></pre>
  
  <p>Here is what I am doing now:</p>
  
  <pre><code>        #FUNCTION TO CLEAN DATA
  function cleanData(&amp;$str)
  {
     // escape tab characters
      $str = preg_replace("/\t/", "\\t", $str);
  
      // escape new lines
      $str = preg_replace("/\r?\n/", "\\n", $str);
  
      // convert 't' and 'f' to boolean values
      if($str == 't') $str = 'TRUE';
      if($str == 'f') $str = 'FALSE';
  
      // force certain number/date formats to be imported as strings
      if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {
        $str = "'$str";
      }
  
      // escape fields that include double quotes
      if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  
  }
  
    $filename = "call_logs_as_of_" . date('Ymd') . ".xls";
  
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");
  
    $flag = false;
    foreach($collectedData as $row) {
      if(!$flag) {
        // display field/column names as first row
        echo implode("\t", array_keys($row)) . "\r\n";
        $flag = true;
      }
       array_walk($row, 'cleanData');
      echo implode("\t", array_values($row)) . "\r\n";
    }
  </code></pre>
  
  <p>This works great, its just not formatted correctly and I am confused on how to accomplish what I am trying to do.  </p>
  
  <p>Suggestions, thoughts?</p>
  
  <p>David</p>