Found 33676 Articles for Programming

Return an array with numeric keys PHP?

AmitDiwan
Updated on 12-Oct-2020 13:50:55

211 Views

For this, use foreach loop along with [] and implement arary_values() method.Example Live Demo OutputArray ( [0] => Array ( [0] => Array ( [LASTNAME] => Doe [FIRSTNAME] => John ) ) [1] => Array ( [0] => Array ( [LASTNAME] => Miller [FIRSTNAME] => David ) ) )

PHP array_push() to create an associative array?

AmitDiwan
Updated on 12-Oct-2020 13:47:40

1K+ Views

To create associative arrays in PHP, use [] brackets. You don't need to use array_push().Example Live Demo OutputArray ( [0] => Array ( [emp_id] => 101 [emp_first_name] => John [emp_last_name] => Doe [emp_country_name] => AUS ) )

Can we define constant in class constructor PHP?

AmitDiwan
Updated on 12-Oct-2020 13:43:11

823 Views

No, you cannot define constant in class constructor, you can use constant at the class level.Let’s say we have the following class −class ConstantDemo { }Define a constant in the class −class ConstantDemo {     const LANGUAGE_NAME="PHP"; } Example Live Demo OutputThe language Name is=PHP

Reference assignment operator in PHP to assign a reference?

AmitDiwan
Updated on 12-Oct-2020 13:41:24

330 Views

Let’s say we have the following value −$nextValue=100;Let us take a new variable and assign a reference −$currentValue = &$nextValue;To assign a reference, use the reference assignment operator i.e. =&$anyVariableName in PHP.The PHP code is as follows −Example Live Demo OutputNext Value=100 Current Value=100 After changing the value of next will reflect the current value because of => Next Value=45000 Current Value=45000

PHP print keys from an object?

AmitDiwan
Updated on 12-Oct-2020 13:35:45

6K+ Views

Let’s say the following is our object −$employeeDetails = (object) [     'firstName' => 'John',     'lastName' => 'Doe',     'countryName' => 'US' ];We want the following output i.e. only the keys −firstName lastName countryNameTo display only the keys from an object, use array_keys() in PHP.Example Live Demo OutputAll Keys are as follows= firstName lastName countryName

Convert timestamp coming from SQL database to String PHP?

AmitDiwan
Updated on 12-Oct-2020 13:26:15

468 Views

To convert timestamp to string, use setTimestamp(). Let’s say the following is our input i.e. timestamp −$SQLTimestamp = 1600320600000;We want the following output i.e. Date string −2020-09-17 05:30:00At first, get seconds from Timestamp −$seconds = round($SQLTimestamp/1000, 0);Example Live Demo Output2020-09-17 05:30:00

How to build dynamic associative array from simple array in php?

AmitDiwan
Updated on 12-Oct-2020 13:20:17

2K+ Views

Let’s say we have the following array −$namesArray = ['John', 'Adam', 'Robert'];We want the following output i.e. an associative array from the above array −Array ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) )Example Live Demo OutputArray ( [John] => Array ( [Adam] => Array ( [Robert] => Smith ) ) ) Doe

How to insert into a table all combination of values in PHP arrays?

AmitDiwan
Updated on 12-Oct-2020 13:14:21

1K+ Views

For this, use the foreach loop and insert into statement in order to insert all combinations of values in PHP arrays.Let’s say we have the following arrays −$name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"];Example Live Demo

Display a two-dimensional array with two different nested loops in matrix form PHP?

AmitDiwan
Updated on 12-Oct-2020 13:07:09

386 Views

Use two loops, one for row and another for column. Fr matrix form, use the tab \t in the nested loop like his −twoDimensionalArray[row][col],"\t";Example Live Demo Output777 777 777Above, you can see the output is in matrix form 3x3.

How to add http:// if it doesn't exist in the URL PHP?

AmitDiwan
Updated on 12-Oct-2020 13:01:17

509 Views

Here, we have set a function that adds "http:// to a string. Let’s say we have passed the following value −example.comAnd the output we want is with "http://" i.e. an actual link −http://example.com For this, you can use dot(.) notation and conditional match with preg_match().Example Live Demo Outputhttp://example.com https://example.com

Advertisements