oracle259 Posted October 8, 2006 Share Posted October 8, 2006 how do i achieve the following. say i have $string1 = "12345";$string2 = "54321";how do i go about combining them to get $string3 = "1524334251";thanks Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 8, 2006 Share Posted October 8, 2006 Use the fact that you can treat strings like arrays:[code]<?php$string1 = "12345";$string2 = "54321";$string3 = '';for($i=0;$i<strlen($string1);$i++) $string3 .= $string1[$i] . $string2[$i];echo $string3;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
oracle259 Posted October 9, 2006 Author Share Posted October 9, 2006 thanks for the tips. Im curious though is there a way to make combination random or somewhat random. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 9, 2006 Share Posted October 9, 2006 Here's the same script with the addition of two real arrays to store the characters of the strings. Then I use the [url=http://www.php.net/shuffle]shuffle()[/url] function to randomize each array before combining them as before.[code]<?php$string1 = "12345";$string2 = "54321";$string3 = '';$str1 = array();$str2 = array();$str3 = '';for($i=0;$i<strlen($string1);$i++) { $str1[] = $string1[$i]; $str2[] = $string2[$i]; $string3 .= $string1[$i] . $string2[$i]; }echo $string3;shuffle($str1);shuffle($str2);for ($i=0;$i<count($str1);$i++) $str3 .= $str1[$i] . $str2[$i];echo '<br>'.$str3;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
oracle259 Posted October 9, 2006 Author Share Posted October 9, 2006 thanxs u guys rock 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.