Array to string conversion in PHPWhile working with PHP, we face many problems related to arrays, where we cannot work on the array directly. To solve it, first we have to convert an array into a string, and then we can efficiently work on a single element. A string is used to store characters in a sequence and represent it as a single entity with a single data type. Thus, data storage is flexible, making it easy to work with, whereas Array stores data as a particular entity, and the data storage is fixed. We will be working on three ways to convert array elements into strings in PHP. 1) Implode () function in PHP:The IMPLODE () function is a built-in PHP function that is mainly used to join all the elements of a declared array. When all the array elements are joined together, they form a string, implode () works similarly to the joint () function in PHP, and return the value as a string. NOTE: - The array is represented in the form of a string, but the base data type remains array. If we use the gettype () function on the converted string, it will still show an array.Here the separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like "," "-" "_" "+" ";" ":" etc. Array stands for the array that will be joined as a string. Example: To convert an array to string. When we execute the code as mentioned above, the compiler will give the output - Array ( [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ) converted array This Array Will Be Converted To A String
Example : To convert an array to string with different parameters. When we execute the code as mentioned above, the compiler will give the output - Default array Array ( [0] => This [1] => Is [2] => An [3] => ARRAY [4] => ! ) Array to string This Is An ARRAY ! This _ Is _ An _ ARRAY_ ! This - Is - An - ARRAY- ! This / Is / An / ARRAY/ ! Data type: - Array
NOTE: We can again convert the string back to array using the explode () functionExample -> to convert string back to array When we execute the code as mentioned above, the compiler will give the output - original string -> we will convert the string back to an array converted array -> Array ( [0] = > we [1] = > will [2] = > convert [3] = > string [4] = > back [5] = > to [6] = > array )
2) Join () function in PHP:The Join () function is a built-in PHP function commonly used to join arrays and return a string. A separator refers to what we have to add between elements of an array while converting it to a string. The default value is an empty string "", but we can add many different values like ", " "-" "_" "+" ";" ":" etc. Array stands for the array that will be joined as a string Example: To convert an array to string using join. When we execute the code as mentioned above, the compiler will give the output - original array Array ( [0] = > This [1] = > Array [2] = > Will [3] = > Be [4] = > Converted [5] = > To [6] = > A [7] = > String ) converted array This Array Will Be Converted To A String This _ Array _ Will _ Be _ Converted _ To _ A _ String
3) Json_encode () function in PHP:The Json_encode () function is a built-in PHP function commonly used to convert arrays and objects in PHP into JSON string (JavaScript Object Notation) representation. NOTE: - JSON is used commonly to retrieve data from web servers and display the same on web pagesArray/object name stands for the array that will be joined as a string. Example -> to convert an array to string using json_encode When we execute the code as mentioned above, the compiler will give the output - {"f-name":"ABC","l-name":"DEF","0":{"email":"[email protected]","mobile":"abcdefghij","0":{"address":"klmnopqrst","city":"OAB","pincode":"110090"}}}
Next TopicPHP Contact Form |