PHP Array Functions

 

PHP Array Functions

The array functions allow you to access and manipulate arrays.

Simple and multi-dimensional arrays are supported.

Function

Description

array()

Creates an array

array_column()

Returns the values from a single column in the input array

array_combine()

Creates an array by using the elements from one "keys" array and one "values" array

array_flip()

Flips/Exchanges all keys with their associated values in an array

array_pop()

Deletes the last element of an array

array_push()

Inserts one or more elements to the end of an array

array_replace()

Replaces the values of the first array with the values from following arrays

array_reverse()

Returns an array in the reverse order

array_search()

Searches an array for a given value and returns the key

array_sum()

Returns the sum of the values in an array

PHP array() Function:

This is used to create an array

Example

Create an indexed array named $cars, assign three elements to it, and then print a text containing the array values:

<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>

PHP array_column() Function:

Returns the values from a single column in the input array

Syntax

array_column(array, column_key, index_key)

Example

Get column of last names from a recordset:

<?php
// An array that represents a possible record set returned from a database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Peter',
    'last_name' => 'Griffin',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Ben',
    'last_name' => 'Smith',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Joe',
    'last_name' => 'Doe',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

 

 

Output:

Array
(
  [0] => Griffin
  [1] => Smith
  [2] => Doe
)

PHP array_combine() Function:

The array_combine() function creates an array by using the elements from one "keys" array and one "values" array.

Note: Both arrays must have equal number of elements!

Syntax

array_combine(keys, values)

Example

Create an array by using the elements from one "keys" array and one "values" array:

<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");

$c=array_combine($fname,$age);
print_r($c);
?>

Output:

Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )

PHP array_reverse() Function

The array_reverse() function returns an array in the reverse order.

Syntax

array_reverse(array, preserve)

Example

Return an array in the reverse order:

<?php
$a=array("a"=>"Volvo","b"=>"BMW","c"=>"Toyota");
print_r(array_reverse($a));
?>

Output:

Array ( [c] => Toyota [b] => BMW [a] => Volvo )

PHP array_replace() Function

The array_replace() function replaces the values of the first array with the values from following arrays.

Syntax

array_replace(array1, array2, array3, ...)

ExampleGet your own PHP Server

Replace the values of the first array ($a1) with the values from the second array ($a2):

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_replace($a1,$a2));
?>

Output:

Array ( [0] => blue [1] => yellow )

PHP array_sum() Function

Definition and Usage

The array_sum() function returns the sum of all the values in the array.

Syntax

array_sum(array)

ExampleGet your own PHP Server

Return the sum of all the values in the array (5+15+25):

<?php
$a=array(5,15,25);
echo array_sum($a);
?>

Output:

45

PHP array_search() Function

The array_search() function search an array for a value and returns the key.

Syntax

array_search(value, array, strict)

Example

Search an array for the value "red" and return its key:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

Output:

a

PHP array_push() Function

The array_push() function inserts one or more elements to the end of an array.

Note: Even if your array has string keys, your added elements will always have numeric keys (See example below).

Syntax

array_push(array, value1, value2, ...)

Example

Insert "blue" and "yellow" to the end of an array:

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

Output:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

PHP array_pop() Function

The array_pop() function deletes the last element of an array.

Syntax

array_pop(array)

Example

Delete the last element of an array:

<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>

Output: Array ( [0] => red [1] => green )

PHP array_flip() Function:

The array_flip() function flips/exchanges all keys with their associated values in an array.

Syntax:  array_flip(array)

Example

Flip all keys with their associated values in an array:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$result=array_flip($a1);
print_r($result);
?>

Output:

Array ( [red] => a [green] => b [blue] => c [yellow] => d )

Comments

Post a Comment

Popular posts from this blog

String Functions in C Language(C Language)

Home Menu Options in Ms Word