Which function is used to convert array to string php?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    We have given an array and the task is to convert the array elements into string. In this article, we are using two methods to convert array to string.

    Method 1: Using implode() function: The implode() method is an inbuilt function in PHP and is used to join the elements of an array. The implode() method is an alias for PHP | join() function and works exactly same as that of join() function.

    Syntax:

    string implode($separator, $array)

    Example:

    PHP

    <?php  

    $arr = array("Welcome","to", "GeeksforGeeks"

        "A", "Computer","Science","Portal");  

    echo implode(" ",$arr);

    ?>

    Output:

    Welcome to GeeksforGeeks A Computer Science Portal

    Method 2: Using json_encode() Function:The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation.
    Syntax:

    string json_encode( $value, $option, $depth )

    Example:

    PHP

    <?php 

    $value = array

        "name"=>"GFG"

        array

            "email"=>""

            "mobile"=>"XXXXXXXXXX"

        

    ); 

    $json = json_encode($value); 

    echo($json); 

    ?> 

    Output:

    {"name":"GFG","0":{"email":"","mobile":"XXXXXXXXXX"}}

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    This code explains how to convert an array into a string in PHP using implode function.

    There are two ways to convert an array into a string in PHP.

    1. Using implode() function
    2. Using json_encode() function

    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. <?php  
    2.   
    3. $arr = array("Hello","students"" ""how","are","you");  
    4.   
    5. echo implode(" ",$arr);  
    6. ?>  

    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

    Which function is used to convert array to string php?

    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. <?php  
    2. $str="Hello Students, How are you";  
    3.   
    4.   
    5. $arr=explode(" ",$str);  
    6.   
    7. print_r($arr);  
    8. ?>  

    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

    Which function is used to convert array to string php?

    Using json() Function

    In PHP, objects can be converted into JSON String by using the PHP function json_encode().

    A common use of JSON is to read data from a web server and display the data on a web page.

    Syntax

    1. json_encode(obj_name);  

    Example

    1. <?php  
    2.   
    3.   
    4. @$myObj->name="Tarun";  
    5. @$myObj->age=20;  
    6. @$myObj->cidy=""Noida";  
    7.   
    8.   
    9.   
    10. $myJSON=json_encode($myObj);  
    11.   
    12. echo($myJSON);  
    13.   
    14. ?>  

    In the above example, we assigned the value to the object variable and then in json_encode()converts the value to the array variable and makes the associative array. 

    Output

    Which function is used to convert array to string php?

    How do I turn an array into a string in PHP?

    There are two ways to convert an array into a string in PHP..
    Using implode() function..
    Using json_encode() function..

    What is the name of function used to convert an array into a string in PHP?

    The implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order.

    Which method converts an array to string?

    Arrays. toString() method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space).

    What is In_array function in PHP?

    The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.