Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Array Functions

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Array Functions

Uploaded by

sachin248124
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

PHP ARRAY

FUNCTIONS
array_combine()
• The array_combine() function creates an array by using one
array for the keys and another for the values.
• array_combine(keys, values)

<?php
$name=array("Manoj","Rahul","Aneesh");
$marks=array("75","89","44");
$c=array_combine($name,$marks);
print_r($c);
?>

OUTPUT:
Array ( [Manoj] => 75 [Rahul] => 89 [Aneesh] => 44 )
array_chunk()
• The array_chunk() function splits an array into chunks of
new arrays.
• array_chunk(array, size, preserve_key)

Parameter Description

array Required. Specifies the array to use

size Required. An integer that specifies the size of each chunk

preserve_key Optional. Possible values:


•true - Preserves the keys
•false - Default. Reindexes the chunk numerically
array_chunk()
<?php
$courses=array("a"=>"PHP", "b"=>"Laravel","c"=>"Node
js","d"=>"HTML","e"=>"CSS","f"=>"ASP.NET");
print_r(array_chunk($courses,2));
?>

OUTPUT:
Array ( [0] => Array ( [0] => PHP [1] => Laravel ) [1] =>
Array ( [0] => Node js [1] => HTML ) [2] => Array ( [0] =>
CSS [1] => ASP.NET ) )
array_chunk()
<?php
$courses=array("a"=>"PHP","b"=>"Laravel","c"=>"Node
js","d"=>"HTML","e"=>"CSS","f"=>"ASP.NET");
print_r(array_chunk($courses,2, true));
?>

OUTPUT:
Array ( [0] => Array ( [a] => PHP [b] => Laravel ) [1] =>
Array ( [c] => Node js [d] => HTML ) [2] => Array ( [e] =>
CSS [f] => ASP.NET ) )
array_count_values()
• The array_count_values() function counts all the values of
an array.
• Syntax is: array_count_values(array)

<?php
$a=array("Block 33","Block 34","Block 34","Block 36","Block
36");
print_r(array_count_values($a));
?>
OUTPUT:
Array ( [Block 33] => 1 [Block 34] => 2 [Block 36] => 2 )
array_diff()
• The array_diff() function compares the values of two (or
more) arrays, and returns the differences.

• This function compares the values of two (or more)


arrays, and return an array that contains the entries from
array1 that are not present in array2 or array3, etc.

• array_diff(array1, array2, array3, ...)


array_diff()
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"magenta","i"=>"seagreen");
$result=array_diff($a1,$a2);
print_r($result);
?>
array_flip()
• The array_flip() function flips/exchanges all keys with their
associated values in an array i.e value become keys and
keys become values.
• Syntax is:- array_flip(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 )
array_flip()
<?php
$a1=array("red","green","blue","yellow");
$result=array_flip($a1);
print_r($result);
?>

OUTPUT:
Array ( [red] => 0 [green] => 1 [blue] => 2 [yellow] => 3 )
array_flip() function
The array_flip() function is used to swap the keys and
values of the array.
Since the original array $a1 only contains values and no
keys are explicitly defined, the keys will be automatically
assigned as 0, 1, 2, and 3 (the index positions).
After flipping, these keys become the values, and the
original values become the keys.
array_intersect()
• The array_intersect() function compares the values of two
(or more) arrays, and returns the matches.
• The array_intersect() function compares the
values of two (or more) arrays, and returns the
matches.
• This function compares the values of two or more
arrays, and return an array that contains the entries
from array1 that are present in array2, array3, etc.

Syntax:- array_intersect(array1, array2, array3, ...)


array_intersect()
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"y
ellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$a3=array("red","blue");
$result=array_intersect($a1,$a2,$a3);
print_r($result);
?>
array_merge()
• The array_merge() function merges one or more arrays
into one array.
• array_merge(array1, array2, array3, ...)

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
$a3=array("c"=>"orange","b"=>"magenta");
print_r(array_merge($a1,$a2,$a3));
?>
array_merge()
• The array_merge() function is used to combine the arrays
$a1, $a2, and $a3.
• When merging arrays with duplicate keys, the value of the
later array's key overwrites the value from the earlier
array's key.
array_merge()
<?php
$a1=array("red","green", "blue");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

OUTPUT:
Array ( [0] => red [1] => green [2] => blue [3] => blue [4] =>
yellow )
array_pop()
• The array_pop() function deletes the last element of an
array.
• array_pop(array)

<?php
$a=array("red","green","blue");
array_pop($a);
print_r($a);
?>
OUTPUT:
Array ( [0] => red [1] => green )
array_pop()
<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
array_pop($a);
print_r($a);
?>

OUTPUT:
Array ( [a] => red [b] => green )
array_push()
• The array_push() function inserts one or more elements
to the end of an array.
• array_push(array, value1, value2, ...)

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>
OUTPUT:
Array ( [0] => red [1] => green [2] => blue [3] => green )
array_push()
<?php
$a=array("a"=>"red","b"=>"green");
array_push($a,"blue","yellow");
print_r($a);
?>

OUTPUT:
Array ( [a] => red [b] => green [0] => blue [1] => yellow )
array_reverse()
• The array_reverse() function returns an array in the
reverse order.
• array_reverse(array, preserve)

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

OUTPUT:
Array ( [c] => Toyota [b] => BMW [a] => Volvo )
array_reverse()
Parameter Description
array Required. Specifies an array
preserve •Optional. Specifies if the function should preserve the keys of
the array or not.
•Possible values:true
•false
array_reverse()
• Pass true value to preserve the key

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

OUTPUT:
Array ( [2] => Toyota [1] => BMW [0] => Volvo )
array_search()
• The array_search() function search an array for a value
and returns the key.

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

OUTPUT:
a
Syntax:-
array_search(value, array, strict)

Parameter Description

value Required. Specifies the value to search for

array Required. Specifies the array to search in


strict •Optional. If this parameter is set to TRUE, then this function will
search for identical elements in the array. Possible values:
•true
•false - Default
When set to true, the number 5 is not the same as the string 5
(See example 2)
array_search()
<?php
$a=array("a"=>"1","b"=>1,"c"=>"1");
echo array_search(1,$a,true);
?>

OUTPUT:
b
Array_slice
• <!DOCTYPE html>
• <html>
• <body>

• <?php
• $a=array("red","green","blue","yellow","brown");
• print_r(array_slice($a,1,2));
• ?>

• </body>
• </html>
array_slice(array, start, length, preserve)
Parameter Description
array Required. Specifies an array
start Required. Numeric value. Specifies where the function will start the
slice. 0 = the first element. If this value is set to a negative number, the
function will start slicing that far from the last element. -2 means start at
the second last element of the array.

length Optional. Numeric value. Specifies the length of the returned array. If
this value is set to a negative number, the function will stop slicing that
far from the last element. If this value is not set, the function will return
all elements, starting from the position set by the start-parameter.

preserve •Optional. Specifies if the function should preserve or reset the keys.
Possible values:
•true - Preserve keys
•false - Default. Reset keys
array_slice()
• The array_slice() function returns selected parts of an array.

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"brown")
;
print_r(array_slice($a,1,2));

echo "<br>";

$a=array("red","green","blue","yellow","brown");
print_r(array_slice($a,1,2,true));
?>
OUTPUT:
Array ( [b] => green [c] => blue )
Array ( [1] => green [2] => blue )
array_column()
• The array_column() function returns the values from a single column in the input
array.
• array_column(array, column_key, index_key)
<?php

$result = array(
array('name'=>'Manoj','cgpa'=>6.7,'status'=>'pass'),
array('name'=>"Shalini",'cgpa'=>9.8,'status'=>'pass'),
array('name'=>'Mani','cgpa'=>3.2,'status'=>'fail')
);

$name = array_column($result, 'name');


print_r($name);
?>
OUTPUT:
Array ( [0] => Manoj [1] => Shalini [2] => Mani )
array_column()
<?php

$result = array(
array('name'=>'Manoj','cgpa'=>6.7,'status'=>'pass'),
array('name'=>"Shalini",'cgpa'=>9.8,'status'=>'pass'),
array('name'=>'Mani','cgpa'=>3.2,'status'=>'fail')
);
$names = array_column($result, 'status', 'name');
print_r($names);
?>
PHP implode() Function

• The implode() function returns a string from the


elements of an array.
<?php
$arr
= array('Hello','World!','Beautiful','Day!'
);
echo implode(" ",$arr);
?>

Output:-
Hello World! Beautiful Day!
implode(separator,array)

Parameter Description

separator Optional. Specifies what to put between the array elements.


Default is “ " (an empty string)

array Required. The array to join to a string


Implode
<?php
$arr
= array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr)."<br>";
echo implode("+",$arr)."<br>";
echo implode("-",$arr)."<br>";
echo implode("X",$arr);
?>
PHP explode() Function

<?php
$str = "Hello world. It's a beautiful
day.";
print_r (explode(" ",$str));
?>

Output:-
Array ( [0] => Hello [1] => world. [2] => It's [3] => a [4] =>
beautiful [5] => day. )
array_filter()
• The array_filter() function filters the values of an
array using a callback function.
• This function passes each value of the input array
to the callback function. If the callback function
returns true, the current value from input is
returned into the result array. Array keys are
preserved.
array_filter()

Parameter Description
array Required. Specifies the array to filter
callbackfunction Optional. Specifies the callback function to use

flag •Optional. Specifies what arguments are sent to


callback:ARRAY_FILTER_USE_KEY - pass key as the only
argument to callback (instead of the value)
•ARRAY_FILTER_USE_BOTH - pass both value and key as
arguments to callback (instead of the value)
in_array() function
• The in_array() function searches an array for a
specific value.
• Note: If the search parameter is a string and the
type parameter is set to TRUE, the search is case-
sensitive.
in_array(search, array,
type)

Parameter Description
search Required. Specifies the what to search for
array Required. Specifies the array to search
type Optional. If this parameter is set to TRUE, the in_array()
function searches for the search-string and specific type in
the array.
<?php
$people = array("Peter", "Joe", "Glenn", "Cleveland", 23);
if (in_array("23", $people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
if (in_array("Glenn",$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}

if (in_array(23,$people, TRUE))
{
echo "Match found<br>";
}
else
{
echo "Match not found<br>";
}
?>

You might also like