PHP Articles

Page 41 of 81

How to check for multidimensional nature of an array in PHP

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

In PHP, you can check if an array is multidimensional by examining whether its first element is itself an array. This approach uses sorting to ensure consistent element ordering, then checks the nature of the first element. Using rsort() with isset() and is_array() The following method sorts the array and checks if the first element is an array − Is the array multi-dimensional? bool(true) Alternative Method A simpler approach without sorting − Single array: bool(false) Multi array: bool(true) How It Works ...

Read More

Removing empty array elements in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 733 Views

To remove empty array elements in PHP, you can use several approaches. The most common methods include using array_filter() or manually iterating through the array with foreach and unset(). Using foreach and unset() This method manually iterates through the array and removes empty elements ? After removing empty values from the array: This 91 102 is a sample Using array_filter() The array_filter() function provides a cleaner approach to remove empty elements ? Original array elements: 10 Filtered array elements: 6 This 91 ...

Read More

Merging duplicate values into multi-dimensional array in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 849 Views

In PHP, you can merge duplicate values into a multi-dimensional array by grouping elements based on a common key. This technique helps organize data and eliminate redundant entries by creating sub-arrays for each unique key value. Example Here's how to group array elements by their 'hobby' field − The grouped array elements are: Array ( [Cycling] => Array ( [0] => Array ...

Read More

Removing duplicate elements from an array in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 871 Views

In PHP, there are several methods to remove duplicate elements from an array. The most common approaches include using array_unique(), array_flip(), or array_keys() with array_count_values(). Using array_flip() The array_flip() function reverses keys and values. When flipped twice, duplicate values are automatically removed since array keys must be unique − The original array contains Array ( [0] => 45 [1] => 65 [2] => 67 [3] => 99 [4] => 81 [5] => 90 ...

Read More

PHP program to find standard deviation of values within an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 976 Views

Standard deviation measures how spread out values are from the average in a dataset. In PHP, you can calculate it by finding the mean, computing variances, and taking the square root of the average variance. Syntax The formula for standard deviation is ? Standard Deviation = sqrt(sum((value - mean)²) / count) Example Here's how to create a function to calculate standard deviation ? The standard deviation of elements in the array is 35.423156268181 How It Works The std_deviation() function works in the following steps ...

Read More

Redirection in PHP

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

The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client). Syntax header($header_value, $replace_value, $http_response_code) Parameters header_value − The header string to be sent replace_value − Optional boolean that indicates whether to replace a previous similar header (default: true) http_response_code − Optional HTTP response code (default: 302 for redirects) Basic Redirection Example Here's how to redirect to another website − The exit statement is crucial ...

Read More

The internal working of the 'foreach' loop in PHP

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 282 Views

The 'foreach' loop in PHP helps in accessing key value pairs within an array. The 'foreach' loop works with arrays only, with the advantage that a loop counter wouldn't need to be initialized. In addition to this, no condition needs to be set that would be needed to exit out of the loop. The 'foreach' loop implicitly does this too. Basic Foreach Syntax The foreach loop has two syntax variations − foreach ($array as $value) { // Process each value } foreach ($array as $key => $value) { ...

Read More

How to delete an element from an array in PHP and re-index the array?

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

In PHP, you can delete an element from an array using the unset() function. However, this creates gaps in the array indices. To re−index the array with consecutive numeric keys, use the array_values() function. Using unset() and array_values() The unset() function removes an element by its key, while array_values() returns a new array with consecutive numeric indices ? Original array: array(5) { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(1) "a" [3]=> string(6) "sample" ...

Read More

How to check if a string contains a specific sub string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 780 Views

To check if a string contains a specific substring in PHP, you can use functions like strpos(), str_contains() (PHP 8+), or substr_count(). The most common approach is using strpos() which returns the position of the first occurrence of a substring. Using strpos() The strpos() function returns the position of the substring or false if not found − The substring is present within the string. Using str_contains() (PHP 8+) A more straightforward method available in PHP 8 and later − Substring found! ...

Read More

What is the significance of '^' in PHP?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 489 Views

The '^' is a bitwise XOR (exclusive OR) operator in PHP that performs bitwise operations on operands. For each bit position, it returns 1 if the bits are different and 0 if they are the same. When used with strings, it operates on the ASCII values of characters. How XOR Works The XOR operator compares each bit of the first operand with the corresponding bit of the second operand − If bits are different: returns 1 If bits are the same: returns 0 Example with Numbers Here's how XOR works with integer values ...

Read More
Showing 401–410 of 802 articles
« Prev 1 39 40 41 42 43 81 Next »
Advertisements