Articles on Trending Technologies

Technical articles with clear explanations and examples

Return an array with numeric keys PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 274 Views

In PHP, you can convert an associative array to an array with numeric keys using the array_values() function. This function returns all the values from an array with numeric indexing, starting from 0. Syntax array_values(array $array) Parameters $array − The input array from which to extract values. Return Value Returns an indexed array containing all the values from the input array with numeric keys starting from 0. Example Here's how to transform an associative array into a numerically indexed array ? The output of the ...

Read More

PHP array_push() to create an associative array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

In PHP, array_push() is designed for indexed arrays and automatically assigns numeric keys. To create associative arrays with custom key-value pairs, use square bracket notation [] or direct assignment instead. Why array_push() Doesn't Work for Associative Arrays The array_push() function appends values to the end of an array with numeric indexes, which destroys associative keys ? Array ( [name] => John [age] => 25 [0] => Developer ) Creating Associative Arrays with Square Brackets Use square bracket ...

Read More

Can we define constant in class constructor PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 903 Views

No, you cannot define constants in a class constructor in PHP. Constants must be declared at the class level using the const keyword. Once declared, class constants are immutable and can be accessed using the scope resolution operator ::. Syntax Class constants are defined using the following syntax − class ClassName { const CONSTANT_NAME = "value"; } Why Constants Cannot Be Defined in Constructors Constants are resolved at compile time and must have fixed values. The constructor runs at runtime when an object is instantiated, making it incompatible with ...

Read More

Reference assignment operator in PHP to assign a reference?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 404 Views

In PHP, the reference assignment operator =& allows you to create a reference to a variable rather than copying its value. When variables are linked by reference, changes to one variable automatically reflect in the other. Syntax To assign a reference, use the reference assignment operator − $variable1 = &$variable2; This creates a reference where both $variable1 and $variable2 point to the same memory location. Example Here's how reference assignment works in practice − Next Value = 100 Current Value = 100 After changing nextValue: Next ...

Read More

PHP print keys from an object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 6K+ Views

In PHP, you can extract keys from an object using the array_keys() function. This is useful when you need to access property names dynamically or iterate through object properties. 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 countryName Using array_keys() Method To display only the keys from an object, use array_keys() after ...

Read More

Convert timestamp coming from SQL database to String PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 547 Views

Converting SQL database timestamps to formatted strings in PHP is a common task when working with date and time data. PHP provides the DateTime class with the setTimestamp() method to handle this conversion efficiently. Understanding SQL Timestamps SQL databases often store timestamps as Unix timestamps in milliseconds. To convert them to readable date strings, you need to first convert milliseconds to seconds, then use PHP's DateTime class ? 2020-09-17 05:30:00 Alternative Method Using date() You can also use the built−in date() function for simpler conversions ? ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

In PHP, you can build a dynamic associative array from a simple array by iterating through the array in reverse order and creating nested associative arrays. This technique is useful when you need to transform a flat array into a hierarchical structure. Syntax The basic approach involves using array_reverse() to process elements from the last to the first, building nested arrays progressively ? function buildDynamicArray($array, $value) { if (empty($array)) { return $value; } ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To insert all combinations of values from PHP arrays into a database table, you can use nested foreach loops to generate the Cartesian product. This technique creates every possible combination of values across multiple arrays. Let's say we have the following arrays − $name1 = ["John", "Mike"]; $name2 = ["David", "Sam"]; $name3 = ["Bob", "Adam"]; Example Here's how to generate SQL INSERT statements for all combinations ?

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 439 Views

In PHP, you can display a two-dimensional array in matrix form using nested loops − one loop for rows and another for columns. Use the tab character \t to create proper spacing between elements. Syntax for ($row = 0; $row < rows; $row++) { for ($col = 0; $col < columns; $col++) { echo $twoDimensionalArray[$row][$col], "\t"; } echo ""; // New line after each row } Example Here's how to create and display a ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 579 Views

In PHP, you can automatically add "http://" to URLs that don't already have a protocol using preg_match() to check if the URL starts with http or https. This is useful when processing user input or validating URLs. Syntax if (!preg_match("~^(?:f|ht)tps?://~i", $url)) { $url = "http://" . $url; } Example The following function checks if a URL starts with a protocol and adds "http://" if missing ? http://example.com https://example.com ftp://files.example.com http://www.tutorialspoint.com How It Works The regular expression ~^(?:f|ht)tps?://~i breaks down as follows: ...

Read More
Showing 22241–22250 of 61,297 articles
Advertisements