Nitsan Bin-Nun wrote: > $string = "xxx xxxx xx xxxxx xx xxx > xxx xxxx xx xxxxx xx xxx"; > $t = explode("\n", $string); > foreach ($t as $k => $v) $t[$k] = explode(" ", $v); > var_dump($t); > > On Mon, Apr 13, 2009 at 8:55 PM, Andres Gonzalez <andres@xxxxxxxxxxxxxxx>wrote: > >> Hi, >> >> I am learning PHP and have a simple question. >> I have a input string in this form: >> >> xxx xxxx xx xxxxx xx xxx >> xx xxxxx x xxx xx xxxxxx >> . >> . >> . >> xx xxx xx xxxx xx xx >> >> each line has 6 words of various lengths, all separated by white space. >> the input string can have any number of lines >> >> I want to put this into a multi-dimensional array, each line an array that >> is an element of an outer array. >> >> I have tried various ways to do this--I have used explode() and >> array_filter() and can get a single line parsed and into an array but I am >> having problems getting a well formed 2 dim array. >> >> What is the easiest way to do this? With all of the PHP array functions, >> there should be an very straight forward way to do this. >> >> Any help would be appreciated. >> >> -Andres >> >> >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > Well in your approach you get a bunch of empty elements where the spaces are. Here are two ways but I'm sure one preg_match_all() without the explodes and loop could do it (some guru will show us): //one way $text = 'xxx xxxx xx xxxxx xx xxx xx xxxxx x xxx xx xxxxxx xx xxx xx xxxx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { $temp = explode(' ', $line); $result[] = array_filter($temp, 'reduce'); } function reduce($var) { return !empty($var); } print_r($result); //another way $text = 'xxx xxxx xx xxxxx xx xxx xx xxxxx x xxx xx xxxxxx xx xxx xx xxxx xx xx'; $lines = explode(PHP_EOL, $text); foreach($lines as $line) { preg_match_all('|([^\s]+)+|', $line, $matches); $result[] = $matches[1]; } print_r($result); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php