Programming Articles - Page 1667 of 3366

How to add a string before each numeric value in an R data frame column?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:03:10

4K+ Views

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Example Live Demoset.seed(111) x1

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

4K+ 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

11K+ 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

1K+ 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

327 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

566 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

952 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

584 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

810 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.

Advertisements