7 WAYS TO CONVERT STRING TO ARRAY IN PHP

Today we are going to learn how khổng lồ convert arrays, integers, objects, and variables to string in PHP. We will learn lớn use various built-in php functions or methods to deal with string conversion.

Bạn đang xem: 7 ways to convert string to array in php


Convert PHP Variable khổng lồ String

I could convert a variable to lớn string in PHP with my eyes shut. Jokes apart, PHP provides lots of pre-built function to khuyến mãi with string conversion, we are going khổng lồ use __toString() method lớn convert variable to lớn string. Let us kiểm tra out the below example:


class convertToString public $str; public function __construct($str) $this->str = $str; public function __toString() return (string) $this->str; $myObj = new convertToString("Days Gone");echo $myObj;?>// Result: Days GoneIn the above example, we have used the __toString() function khổng lồ convert variable khổng lồ string. This function doesn’t take any argument, và it is mostly used with Objects.

Convert String khổng lồ Integer in PHP

Converting string khổng lồ integer or numbers is extremely simple in PHP. PHP programming language supports the type conversion, and it offers numerous functions lớn convert a string into numbers.

Let us have a look on some the string to lớn number conversion php functions with an example below.


The number_format() function in PHP is used khổng lồ convert a string into an integer.

$int = "109.126"; echo number_format($int), " "; echo number_format($int, 2); ?>// Result: 109// Result: 109.13In the next step, we’ll take help of primitive types lớn convert a string into an integer, double or float.


$number = "30061987.0915"; // int echo (int)$number, " "; // double echo (double)$number, " "; // float echo (float)$number, " "; ?>// Result: 30061987// Result: 30061987.0915// Result: 30061987.0915

Convert PHP Array to lớn String

We are using PHP’s implode function to lớn convert array lớn a string. The implode function in php takes two parameters. In the first parameter, we pass the separator. This separator is used lớn separate the arrays, và however, if you don’t pass any argument, then it’ll return an empty separator. In the second parameter, we pass the array object, an array which needs khổng lồ be converted into a string.

Xem thêm: Bà Đẻ Ăn Được Những Loại Bánh Gì Và Không Nên Ăn Những Loại Bánh Gì?

$arrray = <"star wars", "avengers", "the matrix", "ghost rider">; echo implode(" | ", $arrray);?>// star wars | avengers | the matrix | ghost rider

Convert PHP String khổng lồ Arrays

Converting a string into arrays in PHP is pretty straightforward. PHP offers various methods to convert string khổng lồ arrays. We will have a look at explode và str_split built-in php functions. These methods split a string and convert it into arrays, let us kiểm tra out the examples below:


Using PHP’s Explode Function Let us begin with the PHP’s explode() function, this method takes 2 argument khổng lồ convert string into array. The 1st argument takes the delimiter to lớn explode the function, and the 2nd argument takes a string. Khổng lồ know more about php you can visit their official site here.

$string = "The Fountainhead, Dumbo, The Man Who Laughs, 7 Faces of Dr. Lao, Moulin Rouge"; $movies_arr = explode(", ", $string); var_dump($movies_arr);?>/* Result:array(5) <0>=> string(16) "The Fountainhead" <1>=> string(5) "Dumbo" <2>=> string(18) "The Man Who Laughs" <3>=> string(18) "7 Faces of Dr. Lao" <4>=> string(12) "Moulin Rouge"*/Using PHP’s str_split Function khổng lồ Convert String khổng lồ Array The str_split method also allows us lớn convert string to an array. The str_split function takes 2 arguments; in the first argument, we pass the string. In the second argument, we pass the number, và this number refers lớn an array element’s character length. By default, it is set lớn 1.


$string = "Loremipsumdolorsitamet"; $string_split = str_split($string, 3); var_dump($string_split);?>/* Result:array(8) <0>=> string(3) "Lor" <1>=> string(3) "emi" <2>=> string(3) "psu" <3>=> string(3) "mdo" <4>=> string(3) "lor" <5>=> string(3) "sit" <6>=> string(3) "ame" <7>=> string(1) "t"*/

Convert PHP Integer to lớn a String

Converting Integer khổng lồ a string in php is not that difficult, we can use PHP’s built-in strval() function. This function is fully capable of converting string, Integer và double to lớn a string. The important thing is lớn remember about this function is it shouldn’t be used with objects & arrays; if used, then this will return the type name.

The Strval Function Syntax

strval( $var )This function takes one parameter, và that is the numerical value which needs to lớn be converted to lớn string. This function returns the string value as you can see in the below example.

$var = 124.061; echo strval($var); ?> // Result: 124.061

Convert String lớn Date/DateTime

The php offers some useful built-in functions to lớn convert string lớn Date và DateTime. In this example, I’ll show you how to use strtotime(), và getDate() functions to lớn achieve the desired results:

String to Date và DateTime Conversion using Strtotime() Example:

The strtotime() Syntax The strtotime() in php function takes Time/Date and now (optional) parameter and returns the numerical value in seconds since Jan 1, 1970.

strtotime(argument);The getDate() Syntax The getDate() is useful for retrieving date/time information based on the date và time values passed into the function:

Convert PHP Object to String

Now, we will covert a php object into the string using the following approach. We are taking help of __toString() & serialize() function help for converting object to lớn string in php. We’ve already discused toString() function in the above example, let us understand what serialize() function is in php.

The serialize() method returns a string containing a byte-stream representation of any value that can be stored in PHP. Reference: https://www.php.net/manual/en/language.oop5.serialization.php

class NewObject public $name = "John Wick"; public function __toString() return "Movie name is: $this->name "; $OBJECT = new NewObject; echo $OBJECT; echo serialize($OBJECT);?> /* Result: Movie name is: John WickO:9:"NewObject":1:s:4:"name";s:9:"John Wick"; */

Conclusion

In this PHP tutorial, we have learnt how to giảm giá with strings conversion, whether it is arrays, integer, objects, or variables. I hope this tutorial will help you if you liked this tutorial, then don’t forget to nội dung it with others. Thanks for reading, và have a good day!
*

Digamber

I am Digamber, a full-stack developer và fitness aficionado. I created this site to bestow my coding experience with newbie programmers. I love to lớn write on JavaScript, ECMAScript, React, Angular, Vue, Laravel.

Twitter GitHub
Build PHP MySQL 5 Star Rating System using jQuery AJAX PHP 8 AJAX Live Data search with MySQL Tutorial PHP 8 Select2 Multi Select with jQuery AJAX Tutorial PHP 8 JSON Data Encode and Decode Examples PHP 8 server Side khung Validation Tutorial Example Integrate Google reCAPTCHA in PHP 8 liên hệ Form How lớn Create Captcha in PHP tương tác Form Create contact Form in PHP 8 with jQuery Validation PHP 8 Radio Buttons: Get Selected Value and showroom Style How khổng lồ Get Selected Values from Select Option in PHP 8 Get Multiple Values of Selected Checkboxes in PHP 8 Create Pagination in PHP 8 with MySQL và Bootstrap