How to optimize this PHP code that includes SPLIT, ARRAY_WALK and JOIN?
<p>My database contains records that have:</p>

<p>1) First line delimited with double \n\n and the rest delimited with \n:</p>

<pre><code>Introduction 

Line 1
Line 2
</code></pre>

<p>2) Everything delimited with \n:</p>

<pre><code>Line 1
Line 2
Line 3
</code></pre>

<p>3) Nothing delimited:</p>

<pre><code>Introduction 
</code></pre>

<p>My goal is to add ">>" before each line that's delimited with a single \n. The spaghetti dinner that I cooked (and it works) looks as follows:</p>

<pre><code>list($intro, $details) = split("\n\n", $dbInput);
if ($details) {
    $temp = split("\n", $details);
    array_walk($temp, create_function('&amp;$v,$k', '$v = "&gt;&gt; $v";'));
    $dbOutput = "$intro \n\n ".join("\n", $temp);
} else {
    $temp = split("\n", $intro);
    if (count($temp) &gt; 1) {
        array_walk($temp, create_function('&amp;$v,$k', '$v = "&gt;&gt; $v";'));
        $dbOutput = join("\n", $temp);
    } else $dbOutput = $temp[0];
}
</code></pre>

<p>An example of what I want to achieve:</p>

<pre><code>Introduction

&gt;&gt; Line 1
&gt;&gt; Line 2
&gt;&gt; Line 3
</code></pre>

<p>or</p>

<pre><code>&gt;&gt; Line 1
&gt;&gt; Line 2
&gt;&gt; Line 3
</code></pre>

<p>or</p>

<pre><code>Introduction
</code></pre>

<p>QUESTIONS: How would I optimize this code to combine array_walk with split and join in the same statement somehow along the following lines:</p>

<pre><code>$a = join("\n" , array_walk(split("\n", $a), create_function(('&amp;$v,$k', '$v = "&gt;&gt; $v";')) ) );
</code></pre>
