note 26211 modified in function.explode by aidan

From: Date: Thu, 01 Jul 2004 09:59:52 +0000
Subject: note 26211 modified in function.explode by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72255@lists.php.net to get a copy of this message
While explode() and implode() are helpful functions, I found I needed to write additional functions to correctly handle two dimensional arrays. Here are the two functions: These functions based on code from <peter at int8 dot com>. <?php // just as double_explode functions akin to explode, so too does // double_implode to implode function double_implode($first_break, $second_break, $array) { $string = ""; $k=0; $num_length = count($array); foreach ($array as $key => $value) { // if the element of the main array is an array if (is_array($array[$key])) { // append the string of the converted arary to // the string being constructed of the whole // main array $string .= implode($second_break, $array[$key]); } else { // append the element to the string $string .= $array[$key]; } $k++; //change here! // append the delimiting character to the string if ($k<$num_length) $string .= $first_break; } return $string; } // this is a replacement for the internal explode function so that // strings representing 2D arrays (ie matrices) maybe made again // into their array format function double_explode($first_break,$second_break,$string) { // creates array based on the first division character $array = explode($first_break, $string); // loops through the created array in search of any strings // that need to be converted to arrays for ($k=0; $k < count($array); $k++) { // the number of times the second division character // is present $count = substr_count($array[$k], $second_break); // if the second division character is present; ie, // if the string needs to be converted to an array if ($count>0) { // creates array from string and puts it in // the main array where the string was $array[$k] = explode($second_break, $array[$k]); } } return $array; } ?> Notice the "if ($k<$num_length)" check near the bottom. What I found is that without that, any string created would have an extra character at the end, and when exploded would have an extra (empty) array cell at the end. So, now I can do something like if ($arraybeforeimplode==$arrayafterexplode) and they will be equal, whereas before they would test as not being equal. I hope that made sense. Thanks! --was-- Thanks peter at int8 dot com for the functions. I did make one change though. <?php function double_implode($first_break, $second_break, $array) { $string = ""; $k=0; $num_length = count($array); foreach ($array as $key => $value) { // if the element of the main array is an array if (is_array($array[$key])) { // append the string of the converted arary to // the string being constructed of the whole // main array $string .= implode($second_break, $array[$key]); } else { // append the element to the string $string .= $array[$key]; } $k++; //change here! // append the delimiting character to the string if ($k<$num_length) $string .= $first_break; } return $string; } ?> Notice the "if ($k<$num_length)" check near the bottom. What I found is that without that, any string created would have an extra character at the end, and when exploded would have an extra (empty) array cell at the end. So, now I can do something like if ($arraybeforeimplode==$arrayafterexplode) and they will be equal, whereas before they would test as not being equal. I hope that made sense. Thanks! http://php.net/manual/en/function.explode.php

« previous php.notes (#72255) next »