Server Side Programming Articles - Page 1736 of 2650

PHP program to print continuous numbers in the form of triangle

AmitDiwan
Updated on 02-Jul-2020 06:35:25

310 Views

To print continuous numbers in the form of triangle in PHP, the code is as follows −Elements Live DemoOutput1 2 3 4 5 6 7 8 9 10This is similar to generating a star or a number pattern, the only difference being, continuous numbers are generated instead of stars or repeated numbers. The function ‘continuous_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and increment. The relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

PHP program to print the number pattern

AmitDiwan
Updated on 02-Jul-2020 06:34:08

2K+ Views

To print the number pattern in PHP, the code is as follows −Example Live DemoOutput1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6This is similar to generating a star pattern, the only difference being, numbers are generated instead of stars. The function ‘num_pattern’ is defined that takes the limit as a parameter. The limit value is iterated over and the number is printed and relevant line breaks are also generated in between. The function is called by passing this limit value and the relevant output is generated on the console.

PHP program to print a pattern of pyramid

AmitDiwan
Updated on 02-Jul-2020 06:32:21

2K+ Views

Let us see an example to print a pattern of pyramid in PHP −Example Live DemoOutput         *         * *        * * *       * * * *      * * * * *     * * * * * *    * * * * * * *A function named ‘print_pattern’ is defined that iterates through the number of rows through which the pattern needs to be generated. Relevant line breaks are also echoed using the ‘echo’ attribute. The function is called by passing the number of rows for which the pattern needs to be generated.

What are multidimensional associative arrays in PHP? How to retrieve values from them?

AmitDiwan
Updated on 02-Jul-2020 06:30:47

2K+ Views

Multidimensional arrays store multiple arrays whereas associative arrays store key-value pairs as data. Grouped relation between data can be stored in multidimensional associative arrays.Example Live DemoOutputJoe 20An array is defined that contains various attributes of an employee. The employee array is accessed based on the index names and the data is displayed on the console.

How to check for multidimensional nature of an array in PHP

AmitDiwan
Updated on 02-Jul-2020 06:28:53

2K+ Views

The ‘rsort’ function can be used to check if an array is multidimensional or not. It takes one parameter, i.e the array that needs to be checked and returns yes or no depending on the nature of the array.Example Live DemoOutputIs the array multi-dimensional? bool(true)An array is defined that contains string elements. A function named ‘multi_dim’ is defined that sorts the elements of the array using ‘rsort’. The ‘isset’ function is then used to perform ‘AND’ operation on the elements of the array. This would help understand if the array has a single dimension or is multi-dimensional.Read More

Different ways of checking if an array is empty or not in PHP

AmitDiwan
Updated on 02-Jul-2020 06:27:29

558 Views

Using the ‘sizeof’ functionLet us see an example −Example Live DemoOutputThe array is empty!An array can be checked to see if it is empty or not in multiple ways. One method is to use the ‘sizeof’ function that sees if the array is empty. If yes, the size would be 0, thereby confirming the array being empty.Using the ‘empty’ functionExample Live DemoOutputThe array is non-empty The array is empty!Another method to check if an array is empty or not is to use the ‘empty’ function, that checks to see the contents of the array, and if nothing is present, says it is ... Read More

Removing empty array elements in PHP

AmitDiwan
Updated on 02-Jul-2020 06:23:49

697 Views

To remove empty array elements in PHP, the code is as follows −Example Live DemoOutputAfter removing null values from the array, the array has the below elements -This 91 102 is a sampleAn array is defined, that contains strings, numbers and ‘null’ values. The ‘foreach’ loop is used to iterate over the elements and if a value is empty, i.e. it contains null, then it is deleted from the array. The relevant array is displayed again that wouldn’t contain null values.

Merging duplicate values into multi-dimensional array in PHP

AmitDiwan
Updated on 01-Jul-2020 08:18:46

796 Views

To merge duplicate values into multi-dimensional array in PHP, the code is as follows −Example Live DemoOutputThe unique array elements are Array (    [Cycling] => Array    (       [0] => Array       (          [Age] => 23          [name] => Joe          [hobby] => Cycling       )       [1] => Array       (          [Age] => 30          [name] => Dev          [hobby] => Cycling       ) ... Read More

Removing duplicate elements from an array in PHP

AmitDiwan
Updated on 01-Jul-2020 08:30:36

832 Views

The ‘array_flip’ function can be used, that will reverse the values as indices and keys as values.Example Live DemoOutputThe original array contains Array (    [0] => 45    [1] => 65    [2] => 67    [3] => 99    [4] => 81    [5] => 90    [6] => 99    [7] => 45    [8] => 68 ) The array after removing duplicate elements is Array (    [0] => 45    [1] => 65    [2] => 67    [3] => 99    [4] => 81    [5] => 90    [6] => 68 )An array ... Read More

PHP program to find missing elements from an array

AmitDiwan
Updated on 01-Jul-2020 08:32:02

1K+ Views

The ‘array_diff’ function can be used to find the elements that are missing from the array.Example Live DemoOutputElements missing from first array are Array (    [1] => 46    [2] => 47    [4] => 49    [5] => 50    [9] => 54    [10] => 55 ) Elements missing from second array are Array (    [1] => 100    [3] => 102    [4] => 103 )A function named ‘absent’ is defined that checks to see the minimum number and the maximum number and generates an array within that range. The function then returns the difference between this array and the original array, using the ‘array_diff’ function, that gives the missing elements from the array.

Advertisements