Jump to content

combine arrays


aspnetguy

Recommended Posts

I have 2 arrays ($arr1, $arr2). Both are different lengths in most cases but it is possible they could be the same length as well.What I want to do it combine these 2 arrays into a string following this pattern ($str = $arr1[0] . $arr2[0] . $arr1[1] . $arr2[1] ...etc,etc)I have made a couple attempts but my brian seems to be fried, being Monday and all, so if you could help I would appreciate it.

Link to comment
Share on other sites

figured it out.

$str = "ilikejello";$salt = "cherry";$str2 = "";$arr1 = string_split($str);$arr2 = string_split($salt);$i=0;$loop = (sizeof($arr1) > sizeof($arr2)) ? sizeof($arr1) : sizeof($arr2);while($i < $loop){  if(isset($arr1[$i]))	$str2 .= $arr1[$i];  if(isset($arr2[$i]))	$str2 .= $arr2[$i];  $i++;}echo $str2;function string_split($str){	   $str_array=array();	   $len=strlen($str);	   for($i=0;$i<$len;$i++) $str_array[]=$str{$i};	   return $str_array;}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...