You are on page 1of 3

Multi - Sorting with array_multisort()

array_multisort() lets you sort multiple related arrays at the same time,
preserving the relationship between the arrays. To use it, simply pass in a list of
all the arrays you want to sort:
array_multisort( $array1, $array2, ... );
$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
$titles = array( “The Grapes of Wrath”, “The Trial”, “The Hobbit”, “A Tale of
Two Cities” );
$pubYears = array( 1939, 1925, 1937, 1859 );
array_multisort( $authors, $titles, $pubYears );
print_r ( $authors );
print_r ( $titles );
print_r ( $pubYears );

Adding and Removing Array Elements


1. Array_unshift()– – Adds one or more new elements to the start of an array.
Array_unshift() to insert an element or elements at the start of an array.
Just pass the array, followed by one or more elements to add. The function
returns the new number of elements in the array. (For Numeric/index
array)
$authors = array( "Steinbeck", "Kafka", "Tolkien", "Dickens" );
echo array_unshift( $authors, "Hardy", "Melville" );
echo "<br>";
print_r($authors);

2. array_push() — Adds one or more new elements to the end of an array


(and also tells you the new length of the array). (For Numeric/index array)
$authors = array( "Steinbeck", "Kafka", "Tolkien", "Dickens" );
echo array_push( $authors, "Hardy", "Melville" );
print_r($authors);

3. array_shift()– – Removes the first element from the start of an array and
returns its value (but not its key). To use it, pass the array in question to
array_shift(): (for both index and associative)

$myBook = array( “title” = > “The Grapes of Wrath”, “author” = > “John
Steinbeck”, “pubYear” = > 1939 );
echo array_shift( $myBook ) . “ < br/ > ”;
print_r( $myBook );

4. array_pop() — Removes the last element from the end of an array. (for
both index and associative)

$myBook = array( “title” = > “The Grapes of Wrath”, “author” = > “John
Steinbeck”, “pubYear” = > 1939 );
echo array_pop( $myBook ) . “< br/ >”;
print_r( $myBook );

5. array_merge()—If you want to join two or more arrays together to produce


one big array, you need the array_merge()
function.

Index
$authors = array( “Steinbeck”, “Kafka” );
$moreAuthors = array( “Tolkien”, “Milton” );
print_r( array_merge( $authors, $moreAuthors ) );

Associative

$myBook = array( “title” => “The Grapes of Wrath”,“author” = > “John


Steinbeck”,“pubYear” = > 1939 );
$myBook = array_merge( $myBook, array( “numPages” = > 464 ) );
print_r ( $myBook );

Extracting a Range of Elements with array_slice()


PHP has a built - in function, array_slice(), that you can use to extract a
range of elements from an array. To use it, pass in the array to extract the
slice from, followed by the position of the first element in
the range (counting from zero), followed by the number of elements to
extract. The function returns a new
array containing copies of the elements you extracted (it doesn ’ t touch
the original array).
$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
$authorsSlice = array_slice( $authors, 1, 2 );
print_r( $authorsSlice );

Converting Between Arrays and Strings


To convert a string to an array, you can use PHP ’ s handy explode() string
function. This function takes
a string, splits it into separate chunks based on a specified delimiter string,
and returns an array
containing the chunks.
$fruitString = "apple,pear,banana,strawberry,peach";
$fruitArray = explode(",", $fruitString);
print_r($fruitArray);

To convert a string to an array, you can use PHP ’ s handy implode() string
function.

$authors = array( "Steinbeck", "Kafka", "Tolkien", "Dickens" );

$new= implode(",", $authors);

print_r($new);

You might also like