Found 1046 Articles for PHP

The easiest way to concatenate two arrays in PHP?

AmitDiwan
Updated on 13-Oct-2020 08:50:40

3K+ Views

Simply use, array_merge() to concatenate two arrays in PHP. Let’s say the following are out arrays −$nameArray1 = array('John','David'); $nameArray2 = array('Mike','Sam');Now, set both the above arrays in array_merge() to concatenate them.The syntax is as follows −array_merge($yourFirstArrayName, $yourSecondArrayName);ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputArray ( [0] => John [1] => David [2] => Mike [3] => Sam )

PHP make sure string has no whitespace?

AmitDiwan
Updated on 13-Oct-2020 08:48:42

3K+ Views

To check whether a string has no whitespace, use the preg_match() in PHP.The syntax is as follows preg_match('/\s/',$yourVariableName);ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe name (John Smith) has the space

How to add time in minutes in datetime string PHP?

AmitDiwan
Updated on 13-Oct-2020 08:46:42

8K+ Views

For this, you can use the strtotime() method.The syntax is as follows −$anyVariableName= strtotime('anyDateValue + X minute');You can put the integer value in place of X.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output2020-10-30 10:15:20

How to count values from a PHP array and show value only once in a foreach loop?

AmitDiwan
Updated on 13-Oct-2020 08:44:41

860 Views

Let’s say the following is our PHP array $listOfNames = array('John','David','Mike','David','Mike','David');We want the output to display the count of values in the above array like this −Array ( [John] => 1 [David] => 3 [Mike] => 2 )To get the count, use inbuilt function array_count_values().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [John] => 1 [David] => 3 [Mike] => 2 )

How to remove a character (‘’) in string array and display result in one string php?

AmitDiwan
Updated on 13-Oct-2020 08:41:18

273 Views

Let’s say the following is our string array −$full_name= '["John Doe","David Miller","Adam Smith"]';We want the output in a single string −John Doe, David Miller, Adam SmithFor this, use json_decode().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe Result in one string=John Doe, David Miller, Adam Smith

Instance child class in abstract static method PHP?

AmitDiwan
Updated on 13-Oct-2020 08:40:06

439 Views

For this, use $anyObjectName=new static() along with self.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputJohn Doe

Generate array of random unique numbers in PHP?

AmitDiwan
Updated on 13-Oct-2020 08:37:21

744 Views

For random unique numbers array, use range() along with shuffle().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )

Combine three strings (day, month, year) and calculate next date PHP?

AmitDiwan
Updated on 13-Oct-2020 08:35:44

440 Views

You need to iterate using three for loops and lookup in given day, month and year. If the day, month and year is available, then put them into a variable.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe next date value is =2018-05-25

Remove new lines from a string and replace with one empty space PHP?

AmitDiwan
Updated on 13-Oct-2020 08:10:30

642 Views

Let’s say the following is our string with new line $sentence = " My name is John Smith My Favorite subject is PHP. ";We need to remove the new line above and replace with a whitespace i.e. the output should be −My name is John Smith My Favourite subject is PHP.For this, use trim() and withing that preg_replace() to replace.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputMy name is John Smith My Favorite subject is PHP.

How do I split the letters and numbers to two arrays from a string in PHP

AmitDiwan
Updated on 13-Oct-2020 08:07:07

422 Views

For this, use preg_split() along with array_shift() and array_pop(). Let’s say the following is our string with letters and numbers $values = "5j4o5h8n";We want to display the numbers and letters in separate arrays.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => j [1] => o [2] => h [3] => n ) Array ( [0] => 5 [1] => 4 [2] => 5 [3] => 8 )

Advertisements