As of the official String.prototype.split() method there exist no way to start splitting a string from index 1 or for general from any index n, but with a little tweak in the way we use split(), we can achieve this functionality.We followed the following approach −We will create two arrays −One that is splitted from 0 to end --- ACTUALSecond that is splitted from 0 TO STARTPOSITION --- LEFTOVERNow, we iterate over each element of leftover and splice it from the actual array. Thus, the actual array hypothetically gets splitted from STARTINDEX to END.Exampleconst string = 'The quick brown fox ... Read More
To flat a JavaScript array of objects into an object, we created a function that takes array of object as only argument. It returns a flattened object with key append by its index. The time complexity is O(mn) where n is the size of array and m is the number of properties in each object. However, its space complexity is O(n) where n is the size of actual array.Example//code to flatten array of objects into an object //example array of objects const notes = [{ title: 'Hello world', id: 1 }, { title: 'Grab a coffee', ... Read More
To find the maximum element in an array, the PHP code is as follows −Example Live DemoOutputThe highest value of the array is91A function named ‘get_max_value()’ is defined, that takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the highest value amongst all of them is given as output −$max_val = $my_array[0]; for ... Read More
To check if a given number is present in an infinite series or not, the PHP code is as follows −Example Live DemoOutputThe number is not present in the infinite seriesAbove, three variables are defined, and the function is called by passing these three values −$m = 3; $n = 5; $o = 9;A function named ‘contains_val’ is defined, that takes these three variables. The first two variables are compared, and if they are equal, ‘true’ is returned. Otherwise, if the different between the first two variables with the product of the third number is greater than 0 and the different ... Read More
To find the first ‘n’ numbers that are missing in an array, the PHP code is as follows −Example Live DemoOutputThe missing values of the array are 1 2 3 4 5In the above code, a function named ‘missing_values´ is defined, that takes the array, length, and the first few numbers that are missing from the array.A variable is assigned to 0 and checked if the first few numbers that need to be found is 0 or more. If it is 0, it is incremented.A count is assigned to 0, and curr value is assigned to 1. Next, the count value ... Read More
To change date format in PHP, the code is as follows −Example Live DemoOutput1970-01-01 00:00:00A function named ‘string_convert’ is used in PHP that takes a date as a parameter. The ‘strtotime’ function is used to convert English text timing into a UNIX timestamp −$sec = strtotime($my_date); $my_date = date("Y-m-d H:i", $sec); $my_date = $my_date . ":00";This date is printed on the screen. Outside the function, the date time is defined, and the function is called on this parameter and the output is displayed on the screen −echo $my_date;
To find the minimum element in an array, the PHP code is as follows −Example Live DemoOutputThe lowest value of the array is 0A function named ‘get_min_value()’ takes an array as parameter. Inside this function, the count function is used to find the number of elements in the array, and it is assigned to a variable −$n = count($my_array);The first element in the array is assigned to a variable, and the array is iterated over, and adjacent values in the array are compared, and the lowest value amongst all of them is given as output −$min_val = $my_array[0]; for ($i = ... Read More
To delete an element from the array using the unset function, the PHP code is as follows −Example Live DemoOutputAfter deleting the element, the array isArray ( [0] => Joe [1] => Ben [2] => Mary [3] => Barun [5] => Mona )Above, an array is defined, with multiple strings −$my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");The unset function is used, by specifying the index of the element in the array that needs to be deleted. After the element is deleted, the output/array is printed on the screen −unset($my_array[4]); print_r("After deleting the element, the array is"); print_r ($my_array);
To find the length of the last word in the string, the PHP code is as follows −Example Live DemoOutputThe length of the last word is 3 The length of the last word is 6A PHP function named ‘last_word_len’ is defined, that takes a string as the parameter −function last_word_len($my_string) { // }The first occurrence of the space inside another string is found by using the ‘strrpos’ function. If that position is present, it is assigned to 0. Otherwise, it is incremented by 1 −$position = strrpos($my_string, ' '); if(!$position){ $position = 0; } else{ $position ... Read More
To find the first word of a sentence, the PHP code is as follows −Example Live DemoOutputThe first word of the string is HiA string is defined at first −$my_string = 'Hi there, this is a sample statement';The ‘strtok’ function is an in-built function that is used to split a string into specified number of parts. Before the first space occurs, the string needs to be split. This split string’s first part is displayed as the output on the screen −echo "The first word of the string is ". strtok($my_string, " ");