Question: What are different type of sorting functions in PHP?
sort() – sort arrays in ascending order.
$sarray = Array([s] => red [x] => blue [3] => orange )
sort($sarray); print_r($sarray);
OUTPUT : Array ( [0] => blue [1] => orange [2] => red )
asort() – sort associative arrays in ascending order, according to the value.
ksort() – sort associative arrays in ascending order, according to the key.
kshort($sarray); print_r($sarray); OUTPUT : Array([s] => red [x] => blue [3] => orange )
arsort() – sort associative arrays in descending order, according to the value.
rsort() – sort arrays in descending order.
krsort() – sort associative arrays in descending order, according to the key.
array_multisort() – sort the multi dimension array.
usort()- Sort the array using user defined function.
Question: Explaine array_merge()
$a1=array(“red”,”green”);
$a2=array(“blue”,”yellow”);
print_r(array_merge($a1,$a2));
Output:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
Note: If you assign only one array to the array_merge() function, and the keys are integers, the function returns a new array with integer keys starting at 0 and increases by 1 for each value (See Example 2 below).
Tip: The difference between this function and the array_merge_recursive() function is when two or more array elements have the same key. Instead of override the keys, the array_merge_recursive() function makes the value as an array.
Exam:
$array1 = array(“color” => “red”, 2, 4);
$array2 = array(“a”, “b”, “color” => “green”, “shape” => “trapezoid”, 4);
print_r(array_merge($array1, $array2));
Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )
$a = [‘1’=>’red’,’2’=>’blue’,’3’=>’orange’,’4’=>’pinks’];
$b = [‘1’=>’red’,’2’=>’blue’,’3’=>’orange’];
print_r(array_merge($a,$b));
OUTPUT :
Array ( [0] => red [1] => blue [2] => orange [3] => pinks [4] => red [5] => blue [6] => orange )
$a = [‘red’=>’1′,’blue’=>’2′,’orange’=>’3′,’pinks’=>’4’];
$b = [‘red’=>’red’,’blue’=>’blue’,’orange’=>’orange’];
print_r(array_merge($a,$b));
OUTPUT :
Array ( [red] => red [blue] => blue [orange] => orange [pinks] => 4 )
Question : Explaine array_search() ?
array_search(value,array,strict)
The array_search() function search an array for a value and returns the key.
$a=array(“a”=>”yellow”,”b”=>”white”,”c”=>”pink”);
echo array_search(“yellow”,$a);
Output : a
Question : Explaine array_combine() ?
The array_combine() function creates an array by using the elements from one “keys” array and one “values” array. > array_combine(keys,values);
$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”);
If number of element is differ in both array than it will show errror;
$a = [‘a’=>’s’,’b’=>’2′,’c’=>’3′];
$b = [‘red’,’d’=>’blue’,’orange’];
print_r(array_combine($a, $b));
OUTPUT :
Array ( [s] => red [2] => blue [3] => orange )
Question : Explaine array_slice()
$arr = array(“hello”, “good”, “fine”, “good”, “fine”, “hello”, “bye”);
print_r(array_slice($arr,0,-2));
Array ( [0] => hello [1] => good [2] => fine [3] => good [4] => fine )
Question : Explaine array_uniqe()
$arr = array(“hello”, “good”, “fine”, “good”, “fine”, “hello”, “bye”);
print_r(array_unique($arr));
Array ( [0] => hello [1] => good [2] => fine [6] => bye );
$arr = array(“2”, “2”, “3”, “3”, “4”, “4”, “5”);
print_r(array_unique($arr));
Array ( [0] => 2 [2] => 3 [4] => 4 [6] => 5 )
Question : Explaine array_diff()
it compare VALUES of array and will show only 1st array values.
$a1=array(“a”=>”1″,”b”=>”1″,”c”=>”blue”,”d”=>”yellow”);
$a2=array(“e”=>”1″,”f”=>”1″,”g”=>”1″,”d”=>”yellow”);
$result=array_diff($a1,$a2);
print_r($result);
Array ( [c] => blue )
Delete array value without foreach :
$messages = [312, 401, 1599, 3, …];
if(($key = array_search($del_val, $messages)) !== false) {
unset($messages[$key]);
}
array_walk : Apply a user supplied function to every member of an array
bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )