Today we'll learn about functions of an array
I have describe Following functions which can used with array
I have describe Following functions which can used with array
- array_change_key_case
Syntax :
array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )
Example :
<?php
$input_array = array("FirSt" => 1, "SecOnd" => 4);
print_r(array_change_key_case($input_array, CASE_UPPER));
?>
Output:
Array ( [FIRST] => 1 [SECOND] => 4 )
- array_chunk
Syntax :
array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )
Example :
<?php
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));
?>
Output :
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[0] => c
[1] => d
)
[2] => Array
(
[0] => e
)
)
Array
(
[0] => Array
(
[0] => a
[1] => b
)
[1] => Array
(
[2] => c
[3] => d
)
[2] => Array
(
[4] => e
)
)
- array_column
Syntax :
array array_column ( array$input
, mixed$column_key
[, mixed$index_key
= null ] )
Example :
<?php
// Array representing a possible record set returned from a database
$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);
$first_names = array_column($records, 'first_name');
print_r($first_names);
?>
Output :
Array
(
[0] => John
[1] => Sally
[2] => Jane
[3] => Peter
) - array_combine
Syntax :
array array_combine ( array$keys
, array$values
)
Example :
<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
print_r($c);
?>
Output :
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
) - array_count_values
Syntax :array array_count_values ( array$array
)
Example :
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Output :Array
(
[1] => 2
[hello] => 2
[world] => 1
) - array_diff_assoc
Syntax :array array_diff_assoc ( array $array1 , array $array2 [, array $... ] )
Example :
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
Output :
Array
(
[b] => brown
[c] => blue
[0] => red
) - array_diff_key
Syntax :
array array_diff_key ( array$array1
, array$array2
[, array$...
] )
Example :
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_key($array1, $array2));
?>
Output :
array(2) {
["red"]=>
int(2)
["purple"]=>
int(4)
} - array_diff_uassoc
Syntax :
array array_diff_uassoc ( array$array1
, array$array2
[, array$...
], callable$key_compare_func
)
Example :
<?php
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);
?>
Output :
Array
(
[b] => brown
[c] => blue
[0] => red
) - array_diff_ukey
Syntax :
array array_diff_ukey ( array$array1
, array$array2
[, array$...
], callable$key_compare_func
)
Example :
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
?>
Output :
array(2)
{
["red"]=>
int(2)
["purple"]=>
int(4)
}
- array_diff
Syntax :
array array_diff ( array$array1
, array$array2
[, array$...
] )
Example :
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Output :
Array
(
[1] => blue
) - array fill()
Syntax :array_fill(index,number,value);
Example :
<?php
$a1=array_fill(3,4,"blue");
$b1=array_fill(0,1,"red");
print_r($a1);
echo "<br>";
print_r($b1);
?>
Array ( [3] => blue [4] => blue [5] => blue [6] => blue )
Array ( [0] => red ) - array_fill_keys()
Syntax :array_fill_keys(keys,value);
Example :
<?php
$keys=array("a","b","c","d");
$a1=array_fill_keys($keys,"blue");
print_r($a1);
?>
Output :
Array ( [a] => blue [b] => blue [c] => blue [d] => blue ) - array_filter() :
Syntax :
array_filter(array,callbackfunction);
Example :
<?php
function test_odd($var)
{
return($var & 1);
}
$a1=array("a","b",2,3,4);
print_r(array_filter($a1,"test_odd"));
?>
Output :
Array ( [3] => 3 ) - array_flip() :
Syntax :array_flip(array);
Example :<?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_intersect()
Syntax :array_intersect(array1,array2,array3...);
Example :
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
?>
Output :
Array ( [a] => red [b] => green [c] => blue ) - array_intersect_assoc()
Syntax :array_intersect_assoc(array1,array2,array3...)
Example :<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");
$result=array_intersect_assoc($a1,$a2);
print_r($result);
?>
Output:
Array ( [a] => red [b] => green [c] => blue ) - array_intersect_key()
Syntax :array_intersect_key(array1,array2,array3...)
Example :<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_intersect_key($a1,$a2);
print_r($result);
?>
Output :Array ( [a] => red [c] => blue ) - array_intersect_uassoc()
Syntax :array_intersect_uassoc(array1,array2,array3...,myfunction)
Example :
<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("d"=>"red","b"=>"green","e"=>"blue");
$result=array_intersect_uassoc($a1,$a2,"myfunction");
print_r($result);
?>
Output :Array ( [b] => green ) - array_intersect_ukey()
Syntax :array_intersect_ukey(array1,array2,array3...,myfunction)
Example :<?php
function myfunction($a,$b)
{
if ($a===$b)
{
return 0;
}
return ($a>$b)?1:-1;
}
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"blue","b"=>"black","e"=>"blue");
$result=array_intersect_ukey($a1,$a2,"myfunction");
print_r($result);
?>
Output :Array ( [a] => red [b] => green ) - array_key_exists()
Synatax :array_key_exists(key,array)
Example :<?php
$a=array("Volvo"=>"XC90","BMW"=>"X5");
if (array_key_exists("Volvo",$a))
{
echo "Key exists!";
}
else
{
echo "Key does not exist!";
}
?>
Output :Key exists! - s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
- s
Comments
Post a Comment