Sort an Array of Dates in PHP

AmitDiwan
Updated on 26-Dec-2019 08:08:28

3K+ Views

To sort an array of dates in PHP, the code is as follows−Example Live DemoOutputThis will produce the following output−Array (    [0] => 2019-08-10    [1] => 2019-09-08    [2] => 2019-10-10    [3] => 2019-11-11 )ExampleLet us now see another example − Live DemoOutputThis will produce the following output−Array (    [0] => 2019-11-11    [1] => 2019-11-11    [2] => 2019-10-10    [3] => 2019-09-08    [4] => 2019-05-11    [5] => 2019-01-01 )

Re-index an Array in PHP

AmitDiwan
Updated on 26-Dec-2019 08:04:03

785 Views

To re-index an array in PHP, the code is as follows −Example Live DemoOutputThis will produce the following output−Array... Key = 0, Value = 150  Key = 1, Value = 100 Key = 2, Value = 120 Key = 3, Value = 110 Key = 4, Value = 115 Array after re-indexing Key = 2, Value = 150 Key = 3, Value = 100 Key = 4, Value = 120 Key = 5, Value = 110 Key = 6, Value = 115ExampleLet us now see another example − Live DemoOutputThis will produce the following output−Array... Key = a, Value = 150 Key = b, Value = 100 Key = c, Value = 120 Key = d, Value = 110 Key = e, Value = 115 Array after re-indexing Key = b, Value = 150 Key = c, Value = 100 Key = d, Value = 120 Key = e, Value = 110 Key = f, Value = 115

Concatenate Two Values from the Same Column with Different Conditions in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:35:39

1K+ Views

For this, you can use group_concat() with aggregate function. Let us first create a table −mysql> create table DemoTable1869      (      Id int,      Subject varchar(20 ),      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1869 values(100, 'MySQL', 'John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(100, 'MongoDB', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(101, 'MySQL', 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1869 values(101, ... Read More

Derive Value of a Field from Another Field in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:31:05

439 Views

For this, you can use the concept of user defined variable. Let us first create a table −mysql> create table DemoTable1868      (      Value int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1868 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(20); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(30); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1868 values(40); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

List All Variables Initialized by SET Operator in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:22:55

310 Views

To list all variables initialized by SET operator, the syntax is as follows −select * from performance_schema.user_variables_by_thread;Here is the query to set the variable −mysql> set @FirstName='John'; Query OK, 0 rows affected (0.00 sec) mysql> set @LastName='Doe'; Query OK, 0 rows affected (0.00 sec)Here is the query to display the list of all variables initialized by SET operator. This list includes the variables set above −mysql> select * from performance_schema.user_variables_by_thread;This will produce the following output −+-----------+---------------+----------------+ | THREAD_ID | VARIABLE_NAME | VARIABLE_VALUE | +-----------+---------------+----------------+ |       120 | TotalAmount   |           5000 | ... Read More

Number Format Function in PHP

Samual Sam
Updated on 26-Dec-2019 07:21:15

329 Views

The number_format() function is used to format a number with grouped thousands.Syntaxnumber_format(num,decimals,decimal_pt,separator)Parametersnum − The number to be formatted.decimals − Specifies how many decimals.decimal_pt − The string to be used for decimal point.separator − Specifies the string to be used for thousands separator.ReturnThe number_format() function returns the formatted number.ExampleThe following is an example − Live DemoOutputThe following is the output −10,000,000ExampleLet us see another example − Live DemoOutputThe following is the output −4,000

Increment Date Time Value by Second with MySQL Query

AmitDiwan
Updated on 26-Dec-2019 07:21:06

573 Views

For this, use date_add() with interval command. Let us first create a table −mysql> create table DemoTable1867      (      ArrivalTime datetime      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1867 values('2019-10-12 12:34:45'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1867 values('2019-10-12 10:04:15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1867 values('2019-10-12 11:00:23'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1867; This will produce the following output ... Read More

Get String Position from One Column in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:19:48

305 Views

For this, use FIND_IN_SET(). Let us first create a table −mysql> create table DemoTable1866      (      Value1 int,      ListOfValues varchar(100)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1866 values(56, '78, 56, 98, 95'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(103, '103, 90, 102, 104'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1866 values(77, '34, 45, 77, 78'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> ... Read More

Round Calculation to Two Decimal Places in MySQL

AmitDiwan
Updated on 26-Dec-2019 07:18:00

525 Views

To round, use MySQL ROUND() function. Let us first create a table −mysql> create table DemoTable1865      (      Value1 int,      Value2 int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1865 values(40, 60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(100, 400); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1865 values(545, 896); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1865;This will produce the ... Read More

Assign MySQL Query Result to a Variable

AmitDiwan
Updated on 26-Dec-2019 07:16:28

1K+ Views

Use @anyVariableName to assign the result of a query into a variable. Let us first create a table −mysql> create table DemoTable1864      (      Id int,      FirstName varchar(20),      LastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1864 values(101, 'Chris', 'Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(102, 'David', 'Miller'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1864 values(103, 'Adam', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

Advertisements