Now, we know What is Array in PHP? and how it work in PHP. After creating an array we need to count values in array, sort arrays by their contents, convert them between strings and arrays, find unique values, search within arrays, compare arrays to other arrays and more.
By using array functions we can easily access and manipulate an array. In following I am going to describe array function with example.
Note that the following array function is mostly used in PHP array to access and manipulate the arrays.
- is_array():
This function is used to find whether a variable is an array or not.
Syntax:
is_array($variable_name);
is_array($variable_name);
Example 1:
<?php
$array=array('x','y','z');
if (is_array($array)){
echo 'Yes! It is an array.';
}else{
echo 'No! It is not an array.';
}
?>
Output of the above program:
Yes! It is an array.
<?php
$array=array('x','y','z');
if (is_array($array)){
echo 'Yes! It is an array.';
}else{
echo 'No! It is not an array.';
}
?>
Output of the above program:
Yes! It is an array.
Example 2:
<?php
$mark = [
'id' => 1027,
'name' => 'John Abhram',
'class' => 'Class ten',
[ 'gender' => 'male',
[
'bangla' => '100',
'english' => '95',
'mathematics' => '85'
],
'country' => 'bangladesh'
]
];
$array1 = is_array($mark);
$array2 = is_array($mark['roll']);
$array3 = is_array($mark[0]);
echo $array1 ? 'The $mark variable is an array<br>' : 'The $mark variable is not an array<br>';
echo $array2 ? 'The "id" key of the $mark variable is an array<br>' : 'The "id" key of the $mark variable is not an array<br>';
echo $array3 ? 'The 0 index of the $mark variable is an array<br>' : 'The 0 index of the $mark variable is not an array<br>';
?>
Output of the above program:
The $mark variable is an array<br>
The "id" key of the $mark variable is not an array<br>
The 0 index of the $mark variable is an array<br>
<?php
$mark = [
'id' => 1027,
'name' => 'John Abhram',
'class' => 'Class ten',
[ 'gender' => 'male',
[
'bangla' => '100',
'english' => '95',
'mathematics' => '85'
],
'country' => 'bangladesh'
]
];
$array1 = is_array($mark);
$array2 = is_array($mark['roll']);
$array3 = is_array($mark[0]);
echo $array1 ? 'The $mark variable is an array<br>' : 'The $mark variable is not an array<br>';
echo $array2 ? 'The "id" key of the $mark variable is an array<br>' : 'The "id" key of the $mark variable is not an array<br>';
echo $array3 ? 'The 0 index of the $mark variable is an array<br>' : 'The 0 index of the $mark variable is not an array<br>';
?>
Output of the above program:
The $mark variable is an array<br>
The "id" key of the $mark variable is not an array<br>
The 0 index of the $mark variable is an array<br>
- in_array():
This function searches in an array for a specific value. It returns TRUE if this specific value is found in the array, or FALSE otherwise.
Syntax:
in_array($value, $array);
in_array($value, $array);
Example :
<?php
$subjects = array("English", "Mathematics", "Biology");
if (in_array("Mathematics", $subjects)) {
echo "Subject Mathematics is exist";
}
if (in_array("Biology", $subjects)) {
echo "Subject mathematics is exist";
}
?>
Output of the above program:
Subject Mathematics is exist
<?php
$subjects = array("English", "Mathematics", "Biology");
if (in_array("Mathematics", $subjects)) {
echo "Subject Mathematics is exist";
}
if (in_array("Biology", $subjects)) {
echo "Subject mathematics is exist";
}
?>
Output of the above program:
Subject Mathematics is exist
- array_unique():
This function helps to remove duplicate values from an array. The first appearance will be kept when two or more array values are the same and the other will be removed.
Syntax:
array_unique($array);
array_unique($array);
Example :
<?php
$subjects = array("array1" => "Bangla", "English", "array2" => "Bangla", "English", "Mathematics");
$result = array_unique($subjects);
print_r($result);
?>
Output of the above program:
Array ( [array1] => Bangla [0] => English [2] => Mathematics )
<?php
$subjects = array("array1" => "Bangla", "English", "array2" => "Bangla", "English", "Mathematics");
$result = array_unique($subjects);
print_r($result);
?>
Output of the above program:
Array ( [array1] => Bangla [0] => English [2] => Mathematics )
- array_search():
This function search an array for a specific value and returns the key. It returns the key if the value found and false otherwise.
Syntax:
array_search($value, $array);
array_search($value, $array);
Example :
<?php
$array = array("x"=>"Bangla","y"=>"Turorial","z"=>"Stars");
print_r(array_search("Turorial", $array));
?>
Output of the above program:
y
<?php
$array = array("x"=>"Bangla","y"=>"Turorial","z"=>"Stars");
print_r(array_search("Turorial", $array));
?>
Output of the above program:
y
- array_reverse():
This array function return an array in reverse order with this array elements.
Syntax:
array_reverse($array);
array_reverse($array);
Example 1:
<?php
$array = array("x"=>"Bangla","y"=>"Turorial","z"=>"Stars");
print_r(array_reverse($array));
?>
Output of the above program:
Array ( [z] => Stars [y] => Turorial [x] => Bangla )
<?php
$array = array("x"=>"Bangla","y"=>"Turorial","z"=>"Stars");
print_r(array_reverse($array));
?>
Output of the above program:
Array ( [z] => Stars [y] => Turorial [x] => Bangla )
Example 2:
$array = [
'PHP array functions',
'What is an array',
'Creating Shopping cart using PHP and MySQLI',
'Laravel Tutorials',
'PHP array function Tutorials'
];
$array1 = array_reverse($array);
foreach ($array1 as $key => $value) {
echo $key . ' - ' . $value . '<br>';
}
Output of the above program:
0 – PHP array function Tutorials
1 – Laravel Tutorials
2 – Creating Shopping cart using PHP and MySQLI
3 – What is an array
4 – PHP array functions
$array = [
'PHP array functions',
'What is an array',
'Creating Shopping cart using PHP and MySQLI',
'Laravel Tutorials',
'PHP array function Tutorials'
];
$array1 = array_reverse($array);
foreach ($array1 as $key => $value) {
echo $key . ' - ' . $value . '<br>';
}
Output of the above program:
0 – PHP array function Tutorials
1 – Laravel Tutorials
2 – Creating Shopping cart using PHP and MySQLI
3 – What is an array
4 – PHP array functions
- array_map():
This function send each value of an array to a function then multiply each value by itself and at last return an array with the new values.
Syntax:
array_map(myfunction, array1, array2, array3, ...)
array_map(myfunction, array1, array2, array3, ...)
Example 1:
<?php
function myfunction($num) {
return($num*$num);
}
$array = array(2,3,4,5,6);
print_r(array_map("myfunction",$array));
?>
Output of the above program:
Array (
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
<?php
function myfunction($num) {
return($num*$num);
}
$array = array(2,3,4,5,6);
print_r(array_map("myfunction",$array));
?>
Output of the above program:
Array (
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
- array_diff():
Syntax:
array_diff($arrayone, $arraytwo)
array_diff($arrayone, $arraytwo)
Example 1:
<?php
$arrayone = [
'Google',
'Microsoft',
'Apple',
'Acer',
'Asus',
'HP',
'NOKIA',
'Samsung',
'SYMPHONY'
];
$arraytwo = [
'Google',
'Microsoft',
'Apple',
'Acer',
'Asus',
'HP',
'NOKIA',
'Samsung'
];
$diff = array_diff($arrayone, $arraytwo);
print_r($diff);
Output of the above program:
Array ( [8] => SYMPHONY )
<?php
$arrayone = [
'Google',
'Microsoft',
'Apple',
'Acer',
'Asus',
'HP',
'NOKIA',
'Samsung',
'SYMPHONY'
];
$arraytwo = [
];
$diff = array_diff($arrayone, $arraytwo);
print_r($diff);
$arrayone = [
'Google',
'Microsoft',
'Apple',
'Acer',
'Asus',
'HP',
'NOKIA',
'Samsung',
'SYMPHONY'
];
$arraytwo = [
'Google',
'Microsoft',
'Apple',
'Acer',
'Asus',
'HP',
'NOKIA',
'Samsung' ];
$diff = array_diff($arrayone, $arraytwo);
print_r($diff);
Output of the above program:
Array ( [8] => SYMPHONY )
Array Counting Functions:
There are some array functions those are related to counting values or dealing with numbers. We can count the number of values in an array and also return the maximum or minimum value and others useful operations.
- count():
Using this function we can know how many values are presented in a array.
Syntax:
count($array);
count($array);
Example 1:
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
print($result);
?>
Output of the above program:
3
<?php
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
print($result);
?>
$a[0] = 1;
$a[1] = 3;
$a[2] = 5;
$result = count($a);
print($result);
?>
Output of the above program:
3
Example 2:
$technology = [
'Google',
'Microsoft',
'ASUS',
'GRAMEEN PHONE',
'BANGLALINK',
'Juniper',
'ACER',
'Samsung',
'SYMPHONY'
];
Output of the above program:
9
$technology = [
'Google',
'Microsoft',
'ASUS',
'GRAMEEN PHONE',
'BANGLALINK',
'Juniper',
'ACER',
'Samsung',
'SYMPHONY'
];
'Google',
'Microsoft',
'ASUS',
'GRAMEEN PHONE',
'BANGLALINK',
'Juniper',
'ACER',
'Samsung',
'SYMPHONY'
];
Output of the above program:
9
- max():
This function finds the maximum value of an array.
Syntax:
max(array);.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
max(array);.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
Example 1:
<?php
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
Output of the above program:
100
<?php
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
Output of the above program:
100
- min():
This function finds the minimum value of an array.
Syntax:
min(array);.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
min(array);.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
or
max(value1, value2, ...);
Parameters:
array − The array with values.
value1, value2 − The values to compare.
Example 1:
<?php
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
Output of the above program:
25
<?php
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
echo (max(80, 75, 95, 100, 25, 65, 35));
?>
Output of the above program:
25
- array_rand():
This function returns one or more random keys from an array.
Syntax:
array_rand(array, number).
Parameters:
array − The specific array.
number − The number of random keys to return.
array_rand(array, number).
Parameters:
array − The specific array.
number − The number of random keys to return.
Parameters:
array − The specific array.
number − The number of random keys to return.
Example 1:
<?php
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array));
?>
Output of the above program:
p
<?php
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array));
?>
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array));
?>
Output of the above program:
p
Example 2:
<?php
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array, 2));
?>
Output of the above program:
Array (
[0] => p
[1] => q
)
<?php
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array, 2));
?>
$array = array("p"=>"Television","q"=>"Computer","r"=>"Radio");
print_r(array_rand($array, 2));
?>
Output of the above program:
Array (
[0] => p
[1] => q
)
[0] => p
[1] => q
)
- array_count_values():
This function count all the values of an array.
Syntax:
array_count_values($array);
array_count_values($array);
Example 1:
<?php
$a=array("Television","Radio","Radio","Television","Computer");
print_r(array_count_values($a));
?>
Output of the above program:
Array (
[Television] => 2
[Radio] => 2
[Computer] => 1
)
<?php
$a=array("Television","Radio","Radio","Television","Computer");
print_r(array_count_values($a));
?>
$a=array("Television","Radio","Radio","Television","Computer");
print_r(array_count_values($a));
?>
Output of the above program:
Array (
[Television] => 2
[Radio] => 2
[Computer] => 1
)
[Television] => 2
[Radio] => 2
[Computer] => 1
)
PHP - Sort Functions For Arrays:
- sort():
Syntax:
sort($array);
sort($array);
Example 1:
<?php
$people = array("American", "China", "Indian");
sort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
Output of the above program:
American
China
Indian
<?php
$people = array("American", "China", "Indian");
sort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
$people = array("American", "China", "Indian");
sort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
Output of the above program:
American
China
Indian
China
Indian
- rsort():
Syntax:
rsort($array);
rsort($array);
Example 1:
<?php
$people = array("American", "China", "Indian");
rsort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
Output of the above program:
Indian
China
American
<?php
$people = array("American", "China", "Indian");
rsort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
Indian
$people = array("American", "China", "Indian");
rsort($people);
$array_length = count($people);
for($i = 0; $x < $array_length; $i++) {
echo $people[$i];
echo "<br>";
}
?>
Output of the above program:
Indian
China
American
- asort():
This function sort associative arrays in ascending order, according to the value.
Syntax:
asort($array);
asort($array);
Example 1:
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
asort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Computer, Value=35k
Key=Laptop, Value=37k
Key=Radio, Value=2k
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
asort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Key=Computer, Value=35k
Key=Laptop, Value=37k
Key=Radio, Value=2k
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
asort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Laptop, Value=37k
Key=Radio, Value=2k
- ksort():
This function sort associative arrays in ascending order, according to the key.
Syntax:
ksort($array);
ksort($array);
Example 1:
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
ksort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Radio, Value=2k
Key=Computer, Value=35k
Key=Laptop, Value=37k
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
ksort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Key=Radio, Value=2k
Key=Computer, Value=35k
Key=Laptop, Value=37k
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
ksort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Computer, Value=35k
Key=Laptop, Value=37k
- arsort():
This function sort associative arrays in ascending order, according to the key.
Syntax:
arsort($array);
arsort($array);
Example 1:
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Laptop, Value=37k
Key=Computer, Value=35k
Key=Radio, Value=2k
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Key=Laptop, Value=37k
Key=Computer, Value=35k
Key=Radio, Value=2k
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Computer, Value=35k
Key=Radio, Value=2k
- krsort():
This function sort associative arrays in descending order, according to the key.
Syntax:
krsort($array);
krsort($array);
Example 1:
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Radio, Value=2k
Key=Laptop, Value=37k
Key=Computer, Value=35k
<?php
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Key=Radio, Value=2k
Key=Laptop, Value=37k
Key=Computer, Value=35k
$prices = array("Computer"=>"35k", "Laptop"=>"37k", "Radio"=>"2k");
arsort($prices);
foreach($prices as $key => $value) {
echo "Key=" . $key . ", Value=" . $value;
echo "<br>";
}
?>
Output of the above program:
Key=Laptop, Value=37k
Key=Computer, Value=35k
Converting Between Arrays and Strings:
- implode()
This function join array elements with a string.
Syntax:
implode(separator, $array);
implode(separator, $array);
Example 1:
<?php
$array = array('Hello','world','This is Tutorial', 'Stars!');
echo implode(" ",$array);
?>
Output of the above program:
Hello world This is Tutorial Stars!
<?php
$array = array('Hello','world','This is Tutorial', 'Stars!');
echo implode(" ",$array);
?>
Hello world This is Tutorial Stars!
$array = array('Hello','world','This is Tutorial', 'Stars!');
echo implode(" ",$array);
?>
Output of the above program:
- explode()
This function break a string into an array.
Syntax:
explode(separator,string,limit)
explode(separator,string,limit)
Example 1:
<?php
$array = "Hello world This is Tutorial Stars";
print_r (explode(" ",$array));
?>
Output of the above program:
Array (
[0] => Hello
[1] => world
[2] => This
[3] => is
[4] => Tutorial
[5] => Stars
)
<?php
$array = "Hello world This is Tutorial Stars";
print_r (explode(" ",$array));
?>
Array (
[0] => Hello
[1] => world
[2] => This
[3] => is
[4] => Tutorial
[5] => Stars
)
$array = "Hello world This is Tutorial Stars";
print_r (explode(" ",$array));
?>
Output of the above program:
[0] => Hello
[1] => world
[2] => This
[3] => is
[4] => Tutorial
[5] => Stars
)
General Manipulation Functions:
- array_key_exists():
This function check if the key exists in an array.
Syntax:
array_key_exists(key, array)
array_key_exists(key, array)
Example 1:
<?php
$array = array("laptop"=>"ASUS PRO BOOK","radio"=>"Radio 2");
if (array_key_exists("laptop",$array)) {
echo "This key exists.";
} else {
echo "This key does not exist.";
}
?>
Output of the above program:
This key exists.
<?php
$array = array("laptop"=>"ASUS PRO BOOK","radio"=>"Radio 2");
if (array_key_exists("laptop",$array)) {
echo "This key exists.";
} else {
echo "This key does not exist.";
}
?>
This key exists.
$array = array("laptop"=>"ASUS PRO BOOK","radio"=>"Radio 2");
if (array_key_exists("laptop",$array)) {
echo "This key exists.";
} else {
echo "This key does not exist.";
}
?>
Output of the above program:
- array_keys():
Syntax:
array_keys($array);
array_keys($array);
Example 1:
<?php
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayKeys = array_keys($array);
print_r($arrayKeys);
?>
Output of the above program:
Array (
[0] => laptop
[1] => television
[2] => motherboard
)
<?php
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayKeys = array_keys($array);
print_r($arrayKeys);
?>
Array (
[0] => laptop
[1] => television
[2] => motherboard
)
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayKeys = array_keys($array);
print_r($arrayKeys);
?>
Output of the above program:
[0] => laptop
[1] => television
[2] => motherboard
)
- array_values():
Syntax:
array_values($array);
array_values($array);
Example 1:
<?php
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayValuues = array_values($array);
print_r($arrayValuues);
?>
Output of the above program:
Array (
[0] => ASUS
[1] => SONY
[2] => GIGABYTE
)
<?php
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayValuues = array_values($array);
print_r($arrayValuues);
?>
Array (
[0] => ASUS
[1] => SONY
[2] => GIGABYTE
)
$array = [ 'laptop' => 'ASUS', 'television' => 'SONY', 'motherboard' => 'GIGABYTE', ];
$arrayValuues = array_values($array);
print_r($arrayValuues);
?>
Output of the above program:
[0] => ASUS
[1] => SONY
[2] => GIGABYTE
)
- array_push():
Syntax:
array_push(array, value1, value2, ...);
array_push(array, value1, value2, ...);
Example 1:
<?php
$array = array("Computer","Laptop");
array_push($array,"Radio","Television");
print_r($array);
?>
Output of the above program:
Array (
[0] => Computer
[1] => Laptop
[2] => Radio
[3] => Television
)
<?php
$array = array("Computer","Laptop");
array_push($array,"Radio","Television");
print_r($array);
?>
Array (
[0] => Computer
[1] => Laptop
[2] => Radio
[3] => Television
)
$array = array("Computer","Laptop");
array_push($array,"Radio","Television");
print_r($array);
?>
Output of the above program:
[0] => Computer
[1] => Laptop
[2] => Radio
[3] => Television
)
- array_pop():
Syntax:
array_pop(array)
array_pop(array)
Example 1:
<?php
$array = array("Computer","Laptop", "Radio");
array_pop($array);
print_r($array);
?>
Output of the above program:
Array (
[0] => Computer
[1] => Laptop
)
<?php
$array = array("Computer","Laptop", "Radio");
array_pop($array);
print_r($array);
?>
Array (
[0] => Computer
[1] => Laptop
)
$array = array("Computer","Laptop", "Radio");
array_pop($array);
print_r($array);
?>
Output of the above program:
[0] => Computer
[1] => Laptop
)
- array_unshift():
Syntax:
array_unshift(array, value1, value2, value3, ...);
array_unshift(array, value1, value2, value3, ...);
Example 1:
<?php
$array = array("p"=>"Computer","q"=>"Radio");
array_unshift($array,"Television");
print_r($array);
?>
Output of the above program:
Array ( [0] => Television [p] => Computer [q] => Radio )
<?php
$array = array("p"=>"Computer","q"=>"Radio");
array_unshift($array,"Television");
print_r($array);
?>
Array ( [0] => Television [p] => Computer [q] => Radio )
$array = array("p"=>"Computer","q"=>"Radio");
array_unshift($array,"Television");
print_r($array);
?>
Output of the above program:
- array_splice():
Syntax:
array_splice(array, start, length, array)
array_splice(array, start, length, array)
Example 1:
<?php
$array1 = array("p"=>"Computer","q"=>"Laptop","r"=>"Radio","s"=>"Television");
$array2 = array("p"=>"Desktop","q"=>"Satteliate"); array_splice($array1,0,2,$array2); print_r($array1);
?>
Output of the above program:
Array ( [0] => Desktop [1] => Satteliate [r] => Radio [s] => Television )
<?php
$array1 = array("p"=>"Computer","q"=>"Laptop","r"=>"Radio","s"=>"Television");
$array2 = array("p"=>"Desktop","q"=>"Satteliate"); array_splice($array1,0,2,$array2); print_r($array1);
?>
Array ( [0] => Desktop [1] => Satteliate [r] => Radio [s] => Television )
$array1 = array("p"=>"Computer","q"=>"Laptop","r"=>"Radio","s"=>"Television");
$array2 = array("p"=>"Desktop","q"=>"Satteliate"); array_splice($array1,0,2,$array2); print_r($array1);
?>
Output of the above program:
- array_merge():
Syntax:
array_merge($array1, $array2);
array_merge($array1, $array2);
Example 1:
<?php
$array1 = [ 'Television', 'Radio', 'Satellite' ];
$array2 = [ 'Telegram', 'Tutorial', 'Stars' ];
$combine = array_merge($array1, $array2);
echo '<pre>';
print_r($combine);
?>
Output of the above program:
Array (
[0] => Television
[1] => Radio
[2] => Satellite
[3] => Telegram
[4] => Tutorial
[5] => Stars
)
<?php
$array1 = [ 'Television', 'Radio', 'Satellite' ];
$array2 = [ 'Telegram', 'Tutorial', 'Stars' ];
$combine = array_merge($array1, $array2);
echo '<pre>';
print_r($combine);
?>
Array (
[0] => Television
[1] => Radio
[2] => Satellite
[3] => Telegram
[4] => Tutorial
[5] => Stars
)
$array1 = [ 'Television', 'Radio', 'Satellite' ];
$array2 = [ 'Telegram', 'Tutorial', 'Stars' ];
$combine = array_merge($array1, $array2);
echo '<pre>';
print_r($combine);
?>
Output of the above program:
[0] => Television
[1] => Radio
[2] => Satellite
[3] => Telegram
[4] => Tutorial
[5] => Stars
)
EmoticonEmoticon