Jump to content

Print elements of an array and concatenate them in a string


Recommended Posts

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?  :)

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)

 

 

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);

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!


$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!

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.