0% found this document useful (0 votes)
13 views2 pages

PHP sop2 functions-1

The document provides an overview of PHP arrays, explaining that they can store multiple values and come in three types: indexed, associative, and multidimensional. It details the array_splice() function, which removes and replaces elements in an array, along with its syntax and parameters. Additionally, it describes the foreach loop, which iterates through each key/value pair in an array.

Uploaded by

dhruvtamboli27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

PHP sop2 functions-1

The document provides an overview of PHP arrays, explaining that they can store multiple values and come in three types: indexed, associative, and multidimensional. It details the array_splice() function, which removes and replaces elements in an array, along with its syntax and parameters. Additionally, it describes the foreach loop, which iterates through each key/value pair in an array.

Uploaded by

dhruvtamboli27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

PHP Arrays

An array stores multiple values in one single variable. An array is a special variable, which can
hold more than one value at a time. An array can hold many values under a single name, and you
can access the values by referring to an index number. In PHP, the array() function is used to
create an array:

In PHP, there are three types of arrays:

 Indexed arrays - Arrays with a numeric index


 Associative arrays - Arrays with named keys
 Multidimensional arrays - Arrays containing one or more arrays

2. PHP array_splice() Function


Definition and Usage

The array_splice() function removes selected elements from an array and replaces it with new
elements. The function also returns an array with the removed elements.

Syntax
array_splice(array, start, length, array)

Parameter Values

Description

array Required. Specifies an array

start Required. Numeric value. Specifies where the function will start removing
elements. 0 = the first element. If this value is set to a negative number, the
function will start that far from the last element. -2 means start at the second last
element of the array.
length Optional. Numeric value. Specifies how many elements will be removed, and
also length of the returned array. If this value is set to a negative number, the
function will stop that far from the last element. If this value is not set, the
function will remove all elements, starting from the position set by the start-
parameter.

array Optional. Specifies an array with the elements that will be inserted to the
original array. If it's only one element, it can be a string, and does not have to be
an array.

3. The PHP foreach Loop

The foreach loop works only on arrays, and is used to loop through each key/value pair in an
array. The foreach loop - Loops through a block of code for each element in an array.

Syntax
foreach ($array as $value) {
code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array
pointer is moved by one, until it reaches the last array element.

You might also like