Verification: a143cc29221c9be0

Php array to string laravel

Php array to string laravel

Using implode() Function

By using the implode() function, we can convert all array elements into a string. This function returns the string. The separator parameter in implode() function is optional. But it is good to use two arguments. 

Syntax

  1. implode(separator,array);  

Example

  1.   
  2. $arr = array("Hello","students"" ""how","are","you");  
  3.   
  4. echo implode(" ",$arr);  
  5. ?>  

In the above example, at first line, an array variable is declared and assigned some values.

In the next line, the implode() function will convert the array into a string. Two parameters are passed in the implode() function. First is the separator and the other one is array_name.

Output

Implode Function

You can also convert that string into an array if required. For that, we can use PHP's explode() function.

explode() function

By using explode() function, we can convert a string into array elements. We can pass three arguments in it. The first one is for separator, second for array_name, and the last one for the limit.

Syntax

  1. explode(separator,array,limit);  

Example 

  1. $str="Hello Students, How are you";  
  2.   
  3.   
  4. $arr=explode(" ",$str);  
  5.   
  6. print_r($arr);  
  7. ?>  

In the above example, a string variable is assigned some value. Then, explode() function breaks this string into an array. After this, we used print_r() function which prints all the array elements and its indexes.

Output

explode function