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! Quote Link to comment 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 Quote Link to comment 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 ?> Quote Link to comment 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 ?> Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.