note 26211 modified in function.explode by aidan

From: Date: Sun, 27 Jun 2004 14:29:40 +0000
Subject: note 26211 modified in function.explode by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71931@lists.php.net to get a copy of this message
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! --was-- Thanks peter at int8 dot com for the functions. I did make one change though. 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 (#71931) next »