g_p_java Posted February 3, 2009 Share Posted February 3, 2009 Hello i have an array which consists of elements, and i would like to print the array without using vardump but as a string since it contains in every index a character e.r. ar[1] = 'a'; ar[2] = ' b'; Print Output shall be: ab and NOT { [0]=> string(1) "a" [1]=> string(1) "b" Then i would like to store the output in an empty string $str = ' '; Then every time i take the elements of the array i shall concatenate them together using a '_'e.g. Print_Array STRING FINAL 1st ab $str = $str.concat(ab.concat('_')) ab_ 2nd cd $str = $str.concat(cd.concat('_')) ab_cd_ 3nd ef $str = $str.concat(kij) ab_cd_ef Is the code fine? May you please help me? Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/ Share on other sites More sharing options...
JonnoTheDev Posted February 3, 2009 Share Posted February 3, 2009 Take a look at the implode() function. e.g. $items = array('a','b','c'); // will produce the string a_b_c print implode("_",$items); Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753616 Share on other sites More sharing options...
Snart Posted February 3, 2009 Share Posted February 3, 2009 In fact, implode() is even normally faster than concatenation. Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753618 Share on other sites More sharing options...
g_p_java Posted February 3, 2009 Author Share Posted February 3, 2009 Thanks for replying, but is there any function for printing all the elements of the array as a string? I mean only the array to print. Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753622 Share on other sites More sharing options...
JonnoTheDev Posted February 3, 2009 Share Posted February 3, 2009 That is what implode() does. I'm not sure what it is your after. You may need to explain a bit better. Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753626 Share on other sites More sharing options...
g_p_java Posted February 3, 2009 Author Share Posted February 3, 2009 That is what implode() does. I'm not sure what it is your after. You may need to explain a bit better. Well thanks for implode, but it's just that firstly i have to print the array and afterwards concatenate that result with _. Then i have to wait for the second print of the array and concatenate the 2nd output of the array with the previous one. ab_cd (ab 1st print of array) (cd 2nd print of the array) Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753638 Share on other sites More sharing options...
JonnoTheDev Posted February 3, 2009 Share Posted February 3, 2009 Then i would suggest storing your data back into another array via your loop and then concatenating using implode(): // build up an array so it looks like $array = array('ab','cd','ef','gh'); // produces ab_cd_ef_gh print implode("_", $array); Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753651 Share on other sites More sharing options...
g_p_java Posted February 3, 2009 Author Share Posted February 3, 2009 Then i would suggest storing your data back into another array via your loop and then concatenating using implode(): // build up an array so it looks like $array = array('ab','cd','ef','gh'); // produces ab_cd_ef_gh print implode("_", $array); Thanks for replying! I have the array $charArray and i do print $charArray; and it doesn't print anything! Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753666 Share on other sites More sharing options...
JonnoTheDev Posted February 3, 2009 Share Posted February 3, 2009 You cannot print an array using print. you can however use print_r(); You must implode the array before it can be printed as a string. Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753673 Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 Thanks for replying! I have the array $charArray and i do print $charArray; and it doesn't print anything! Why not post the code you are using? That way we can specifically show you what to do... Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753680 Share on other sites More sharing options...
g_p_java Posted February 3, 2009 Author Share Posted February 3, 2009 $arr_codes = array(); $arr_codes["0"] = 'j'; $arr_codes["1"] = 'a'; $arr_codes["2"] = 'b'; $arr_codes["3"] = 'c'; $arr_codes["4"] = 'd'; $arr_codes["5"] = 'e'; $arr_codes["6"] = 'f'; $arr_codes["7"] = 'g'; $arr_codes["8"] = 'h'; $arr_codes["9"] = 'i'; var_dump($arr_codes); //Print arr_codes to see if everything is inserted well $encoding_ipPart = array(); //Create new array echo "NEW NEW NEW NEW CODE" . "<br />"; $ip = $_SERVER["REMOTE_ADDR"]; $splitIp = explode(".", $ip); foreach($splitIp as $ipPart){ //echo "SplitIp : , $ipPart " . "<br />"; //SplitIP for($i=0;$i<strlen($ipPart);$i++){ //echo "Take each Character : , $ipPart[$i] " . "<br />"; $charArray[] = $ipPart[$i]; $int_char = $arr_codes[$ipPart[$i]]; // e.g. int_char maybe '9' //echo "Hm... : , $int_char " . "<br />"; $encoding_ipPart[] = $int_char; } echo "Print $encoding ipPart .<br />"; var_dump($encoding_ipPart); //Here prints : e.g. if we have $ipPart = 97 , then $encoding_ipPart shall print id //$array_with_all_ipParts shall store every $encoding_ipPart e.g. it shall look like this $array = array('ab','cd','ef','gh'); $array_with_all_ipParts[] = $encoding_ipPart; $encoding_ipPart = array(); //clear array } var_dump($charArray); print implode("_",$array_with_all_ipParts); //$array_with_all_ipParts shall print di_jk_lo Hope you understand! It's just that my program shall take as input an ip and output a specific string related to the ip that visits the site. Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-753699 Share on other sites More sharing options...
g_p_java Posted February 5, 2009 Author Share Posted February 5, 2009 Hello, i would like to make a program which is going to take as input the client's IP and output a string related to that IP. That program has to tokenize every IP and produce a specific and unique string for that IP , that means that i have to produce for every token a specific string.So, i have to convert every number to a string The algorithm i'm thinking is: have an array of characters like $arr_codes = array(); $arr_codes["0"] = 'j'; $arr_codes["1"] = 'a'; $arr_codes["2"] = 'b'; $arr_codes["3"] = 'c'; $arr_codes["4"] = 'd'; $arr_codes["5"] = 'e'; $arr_codes["6"] = 'f'; $arr_codes["7"] = 'g'; $arr_codes["8"] = 'h'; $arr_codes["9"] = 'i'; e.g. input: 97.172.1.2 Tokenise 97 172 1 2 Output ig_agb_a_b The code i have is: $encoding_ipPart = array(); $array_with_all_ipParts = array(); $j = 0; $ip = $_SERVER["REMOTE_ADDR"]; $splitIp = explode(".", $ip); foreach($splitIp as $ipPart){ for($i=0;$i<strlen($ipPart);$i++){ $charArray[] = $ipPart[$i]; $int_char = $arr_codes[$ipPart[$i]]; $encoding_ipPart[] = $int_char; } $array_with_all_ipParts[$j] = $encoding_ipPart; echo "Array with all ipParts : " . "<br />"; var_dump($array_with_all_ipParts); $j = $j + 1; $encoding_ipPart = array(); } echo "PHP PHPH PHPH HELP " . "<br />"; $new_array = implode(" ",$Whole_String); print $new_array; May you please help me? The program has to take the IP of the client and convert it into a string. But it doesn't output the string as i desire, Also what about the algorithm?Is it a good choice for converting an IP into a unique string, so that everytime a specific IP visits the site, the same string is going to be generated. Quote Link to comment https://forums.phpfreaks.com/topic/143631-print-elements-of-an-array-and-concatenate-them-in-a-string/#findComment-755150 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.