Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First <code>explode()</code> the array with the delimiter <code>*</code></p> <p>You could then use preg_match_all to match each item in the exploded array. Something like this works with your example input.</p> <pre><code>$layout = explode('*', $input); $columns = array(); foreach ( $layout as $item ){ $parts = array(); //matches either a -, x or + followed by one or more digits preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER); foreach ( $matches as $match){ //match[1] hold the + or -, match[2] holds the digits $parts[] = array($match[1], $match[2]); } $columns[] = $parts; } </code></pre> <p>The output from your example ends up like this:</p> <pre><code>array( array( array('+', '2'), array('-', '1'), array('+', '18') ), array( array('+', '7'), array('-', '21'), array('+', '3') ), //etc ); </code></pre> <hr> <p>With PHP 5.3 you could use something like this (untested). The main difference is that the inner loop has been replaced by <a href="http://php.net/manual/en/function.array-map.php" rel="nofollow noreferrer"><code>array_map</code></a> which removes the need for a lot of lines of code. (Array map applies a function to every item in an array and returns the transformed array). PHP 5.3 is required for the nice <a href="http://php.net/manual/en/functions.anonymous.php" rel="nofollow noreferrer">closure syntax</a></p> <pre><code>$layout = explode('*', $input); $columns = array(); foreach ( $layout as $item ){ preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER); $columns[] = array_map( function($a){ return array($a[1], $a[2]); }, $matches); } </code></pre> <p>You could also remove the loops altogether:</p> <pre><code>$innerMatch = function($item){ preg_match_all('/([+-x])(\d+)/', $item, $matches, PREG_SET_ORDER); return array_map( function($a){ return array($a[1], $a[2]); }, $matches); }; $columns = array_map($innerMatch, explode('*', $input)); </code></pre> <p>However this has the large disadvantage of not being very readable to most PHP developers which is why I wouldn't recommend using it.</p> <hr> <h3>More explanation</h3> <p>At the request of <a href="https://stackoverflow.com/users/118989/christopher-altman">@Christopher Altman</a></p> <p>The only new bit in the PHP 5.3 version is really this:</p> <pre><code>array_map( function($a){ return array($a[1], $a[2]); }, $matches ); </code></pre> <p>Expanding and altering it a bit (as an example)</p> <pre><code>//bind an anonymous function to the variable $func $func = function($a){ return $a*$a; }; //$func() now calls the anonymous function we have just defined //then we can call it like so: $result = array_map($func, $myArray); </code></pre> <p>So if <code>$myArray</code> is defined as</p> <pre><code>array(1,2,3,4); </code></pre> <p>When it is run through the array map function you can think of it as converting it into</p> <pre><code>array(func(1),func(2),func(3),func(4)); </code></pre> <p>But as PHP isn't a <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" rel="nofollow noreferrer">lazy language</a>, all the functions are evaluated as soon as they are encountered, so the array is returned from array_map as:</p> <pre><code>array(2, 4, 9, 16) </code></pre> <p>In the actual code, <code>preg_match_all</code> returns an array of matches (where the matches are arrays). So all I do is take the array and on every match apply a function that converts the match into a different array in the required format.</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