$firstName=["gela", "bela", "lela"];
$age = [18, 19, 20];
$keyValue = array_combine($firstName, $age);
print_r($keyValue);
// ( "gela" => 18, "bela" => 19, "lela" => 20 )
// Please note that both arrays should have equal quantity of elements !!!<?php
$a = 0;
// True because $a is set
if (isset($a)) {
echo "Variable 'a' is set.<br>";
}
$b = null;
// False because $b is NULL
if (isset($b)) {
echo "Variable 'b' is set.";
}
?>
$x = 10;
$y = 5;
exit; // Terminates the execution of the script
echo $x + $y; // This line will not be executed
$x = 10;
$y = 5;
exit("terminate the current script"); // Output a message
echo $x + $y; // This line will not be executed
function bubbleSort(&$array)
{
if ($length = count($array)) {
for ($i = 0; $i < $length; $i++) {
for ($k = 0; $k < $length; $k++) {
if ($array[$i] < $array[$k]) { // > descending
$tmp = $array[$i];
$array[$i] = $array[$k];
$array[$k] = $tmp;
}
}
}
}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
bubbleSort($arr);
print_r ($arr); // outputs: 1 2 7 14 15 26 34 66 72 99 923
➤ InsertSort Algorythm.
function insertSort(&$array) {
if ($length = count($array)) {
for ($i = 0; $i < ($length - 1); $i++) {
$key = $i + 1;
$tmp = $array[$key];
for ($k = ($i + 1); $k > 0; $k--) {
if ($tmp < $array[$k - 1]) { // > descending
$array[$k] = $array[$k - 1];
$key = $k - 1;
}
}
$array[$key] = $tmp;
}
}
}
$arr = [2,99,15,72,14,1,26,923,34,66,7];
insertSort($arr);
print_r ($arr); // 1 2 7 14 15 26 34 66 72 99 923
$numbers = array(5, 1, 3, 4, 2);
sort($numbers);
print_r ($numbers); // outputs: 1 2 3 4 5
$cars = array("Ferrari", "BMW", "Mustang");
sort($cars);
print_r ($cars); // outputs: BMW Ferrari Mustang
// ASCII Code 66 - B
// ASCII Code 70 - F
// ASCII Code 77 - M
➤ rsort() - sort arrays in descending order.
$numbers = array(5, 1, 3, 4, 2);
rsort($numbers);
print_r ($numbers); // outputs: 5 4 3 2 1
➤ asort() - sort associative arrays in ascending order, according to the value.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
asort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
// Key = Keanu Reeves -> Value = 58
➤ ksort() - sort associative arrays in ascending order, according to the key.
$age = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
ksort($age);
foreach($age as $key => $value) {
echo "Key = $key -> Value = $value \n";
}
// outputs:
// Key = Keanu Reeves -> Value = 58
// Key = Keira Knightley -> Value = 37
// Key = Mads Mikkelsen -> Value = 56
• arsort() - sort associative arrays in descending order, according to the value.
• krsort() - sort associative arrays in descending order, according to the key.array_count_values(array)
For instance:
$animals = array(
"Cat",
"Cat",
"Dog",
"Rabbit",
"Rabbit",
"Dog",
"Dog",
"Dog",
"Cat",
);
print_r (array_count_values($animals));
/* outputs:
Array
(
[Cat] => 3
[Dog] => 4
[Rabbit] => 2
)
*/
unset(variable, ....); // second parameter is optional, another variable to check
For instance:
$x = 10;
unset($x);
// False because $x is NULL
if (isset($x)) {
echo "Variable 'x' is set\n";
}
// Declare an array
$array = array('PHP'=> true);
unset($array['PHP']);
echo isset($array['PHP']) ? 'Array is set.' :
'Array is not set.';
// outputs: Array is not set.
isset(variable, ....); // second parameter is optional, another variable to check
For instance:
$x = 0;
// True because $x is set
if (isset($x)) {
echo "Variable 'x' is set\n";
}
$y = null; // or $y;
// False because $y is NULL
if (isset($y)) {
echo "Variable 'y' is set.";
// Declare an array
$array = array('PHP'=> true);
// Use isset function
echo isset($array['PHP']) ? 'Array is set.' :
'Array is not set.';
// outputs: Array is set.
}
function print_string( &$str ) {
$str = " PHP \n";
print( $str );
}
$string = " JS \n";
print_string( $string ); // outputs: PHP
print( $string ); // outputs: PHP
$x = 10;
$y = 20;
$y = &$x;
echo " x = $x \n"; // outputs: x = 10
echo " y = $y \n\n"; // outputs: y = 10
$y = 20;
echo " x = $x \n"; // outputs: x = 20
echo " y = $y \n"; // outputs: y = 20