eldan88 Posted March 27, 2012 Share Posted March 27, 2012 Hi, I am a little bit new to php, and I am learning about array functions. I just wanted to know the difference between implode and explode in arrays, and how each of them would be used? Thank you! Link to comment https://forums.phpfreaks.com/topic/259778-array-functions-whats-the-difference-between-implode-and-explode/ Share on other sites More sharing options...
l0gic Posted March 27, 2012 Share Posted March 27, 2012 Well explode breaks a string into many array elements, and implode builds a string from array elements. Like so.. <?php $str = "1,2,3,4,5"; echo $str; // would display "1,2,3,4,5" $explodedstring = explode(",",$str); echo $explodedstring[0]; // would display the first array element in this case "1" echo $explodedstring[4]; // would display the fifth array element in this case "5" $implodedstring = implode("-",$explodedstring); echo $implodedstring; // would display "1-2-3-4-5" ?> More here: http://php.net/manual/en/function.explode.php http://php.net/manual/en/function.implode.php Link to comment https://forums.phpfreaks.com/topic/259778-array-functions-whats-the-difference-between-implode-and-explode/#findComment-1331427 Share on other sites More sharing options...
eldan88 Posted March 27, 2012 Author Share Posted March 27, 2012 Oh, I understand now! But isn't the explode function similar to just calling a position within the array such as the following example... <?php $array1 = array(4,8,15,16,23,42); ?> <?php echo $array1[1] // Returns 8 ?> Link to comment https://forums.phpfreaks.com/topic/259778-array-functions-whats-the-difference-between-implode-and-explode/#findComment-1331554 Share on other sites More sharing options...
seanlim Posted March 27, 2012 Share Posted March 27, 2012 Nope. The explode function breaks up a string. In your example, $array1 is an array. If you had a string "4,8,15,16,23,42", you could call the explode function on it to break it down into the equivalent of $array1. i.e. <?php $array1 = array(4,8,15,16,23,42); $string = "4,8,15,16,23,42"; $array2 = explode($string); //$array1 is the same as $array2 echo $array1[1]; // 8 echo $array2[1]; // 8 echo $string[1]; // will not work as expected ?> Link to comment https://forums.phpfreaks.com/topic/259778-array-functions-whats-the-difference-between-implode-and-explode/#findComment-1331556 Share on other sites More sharing options...
eldan88 Posted March 27, 2012 Author Share Posted March 27, 2012 Okay. I got it now. It was just a little bit confusing because explode and implode is still part of array function.Thank you! Link to comment https://forums.phpfreaks.com/topic/259778-array-functions-whats-the-difference-between-implode-and-explode/#findComment-1331558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.