note 84269 deleted from function.explode by tularis

From: Date: Mon, 27 Oct 2008 13:02:51 +0000
Subject: note 84269 deleted from function.explode by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145806@lists.php.net to get a copy of this message
Note Submitter: webmaster at tubo-world dot de ---- <?php /* This function recursively explodes a string at the boundaries of the strings within the array delimiters One possible scenario: You concatenated rows and columns with GROUP_CONCAT() in MySQL and want to access the values in an array in PHP @author Tobias Schultze @date 2008-07-07 */ function explode_recursive($delimiters, $string) { if (!is_array($delimiters)) { $delimiters = array($delimiters); } $delimiter = array_shift($delimiters); if (!empty($delimiters)) { $arExploded = explode($delimiter, $string); if ($arExploded !== false) { foreach ($arExploded as $key => $value) { $arExploded[$key] = explode_recursive($delimiters, $value); } } return $arExploded; } else { return explode($delimiter, $string); } } // Examples: $strSimpleText = 'row 0 column 0--row 0 column 1--row 0 column 2__row 1 column 0--row 1 column 1--row 1 column 2__row 2 column 0--row 2 column 1--row 2 column 2'; $arSimpleDelimiters = array('__','--'); var_dump(explode_recursive($arSimpleDelimiters, $strSimpleText)); $strComplexText = 'item 0.0.0,item 0.0.1,item 0.0.2;item 0.1.0,item 0.1.1;item 0.2.0,item 0.2.1,item 0.2.2,item 0.2.3|item 1.0.0;item 1.1.0|item 2.0.0,item 2.0.1|item 3.0.0;item 3.1.0,item 3.1.1'; $arComplexDelimiters = array('|',';',','); var_dump(explode_recursive($arComplexDelimiters, $strComplexText)); // Output: array(3) { [0]=> array(3) { [0]=> string(14) "row 0 column 0" [1]=> string(14) "row 0 column 1" [2]=> string(14) "row 0 column 2" } [1]=> array(3) { [0]=> string(14) "row 1 column 0" [1]=> string(14) "row 1 column 1" [2]=> string(14) "row 1 column 2" } [2]=> array(3) { [0]=> string(14) "row 2 column 0" [1]=> string(14) "row 2 column 1" [2]=> string(14) "row 2 column 2" } } array(4) { [0]=> array(3) { [0]=> array(3) { [0]=> string(10) "item 0.0.0" [1]=> string(10) "item 0.0.1" [2]=> string(10) "item 0.0.2" } [1]=> array(2) { [0]=> string(10) "item 0.1.0" [1]=> string(10) "item 0.1.1" } [2]=> array(4) { [0]=> string(10) "item 0.2.0" [1]=> string(10) "item 0.2.1" [2]=> string(10) "item 0.2.2" [3]=> string(10) "item 0.2.3" } } [1]=> array(2) { [0]=> array(1) { [0]=> string(10) "item 1.0.0" } [1]=> array(1) { [0]=> string(10) "item 1.1.0" } } [2]=> array(1) { [0]=> array(2) { [0]=> string(10) "item 2.0.0" [1]=> string(10) "item 2.0.1" } } [3]=> array(2) { [0]=> array(1) { [0]=> string(10) "item 3.0.0" } [1]=> array(2) { [0]=> string(10) "item 3.1.0" [1]=> string(10) "item 3.1.1" } } } ?>

« previous php.notes (#145806) next »