Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
➤ The ord() function returns the ASCII value of the first character of a string. Syntax:
ord(string)
For instance:
echo ord("A")."  -  A\n";  // outputs:   65
echo ord("a")."  -  a\n";  // outputs:   97
echo ord("z")." -  z\n";  // outputs:   122
echo ord(7)."  -  7\n";    // outputs:   55
echo ord("7")."  - '7'\n";  // outputs:   55
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ The print_r() function prints the information about a variable in a more human-readable way. Syntax:
print_r(variable, return);  // second parameter is optional 
For instance:
$cars = array("BMW", "Ferrari", "Mustang");
print_r($cars);
$names = array("Keira Knightley"=>"37", "Mads Mikkelsen"=>"56", "Keanu Reeves"=>"58");
print_r($names);
/* outputs:
Array
(
    [0] => BMW
    [1] => Ferrari
    [2] => Mustang
)
Array
(
    [Keira Knightley] => 37
    [Mads Mikkelsen] => 56
    [Keanu Reeves] => 58
)
*/
by Levani Makhareishvili
2 years ago
0
PHP
Function
1
➤ The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first appearance will be kept and the other will be removed. Syntax:
array_unique(array, sorttype)  // second parameter is optional
For instance:
$colours = [
	"Red",
	"Black", 
	"White",
	"Red",
	"Black",
	"Blue",
	"white"
	];
print_r (array_unique($colours));
// outputs: 
/*Array
(
    [0] => Red
    [1] => Black
    [2] => White
    [5] => Blue
    [6] => white
)*/
by Levani Makhareishvili
2 years ago
0
PHP
Arrays
Function
1
➤ The in_array() function searches an array for a specific value. Syntax:
in_array(search, array, type) // third parameter is optional 
For instance:
$colours = ["Red", "Black", "White"];

if (in_array("Black", $colours))
  echo "Match found!";
else
  echo "Match not found!";
  // outputs:     Match found!
by Levani Makhareishvili
2 years ago
0
PHP
Arrays
Function
1
// Given two integer arrays nums1 and nums2, return an array of their intersection. // Each element in the result must be unique and you may return the result in any order.
function intersection($nums1, $nums2) {
    // firstly, remove duplicates, 
    // you can use here array_unique function twice !
    $result1 = [];
    $result2 = [];
    foreach ($nums1 as $num1) {
    	if (!in_array($num1, $result1)) {
    		$result1[] = $num1;
        }
    }
    foreach ($nums2 as $num2) {
    	if (!in_array($num2, $result2)) {
    		$result2[] = $num2;
        }    
    }
    // merge two arrays
    // you can use here array_merge function !
    for ($i = 0;  $i < count($result2);  $i++) {
        $result1[$i + count($result1)] = $result2[$i];
    }
    $array = [];
    // unfortunately, I could not count the values ​​and 
    // get the keys from array without built-in function
    $result1 = array_count_values($result1);
    foreach ($result1 as $key => $value) {
    	if ($value >= 2) {
    		$array[] = $key;
    	}
    }
    return $array;
}
print_r(intersection([4, 9, 5], [9, 4, 9, 8, 4]));   // [4, 9];
// Instead of so much effort, you can just use the array_intersect() function.
by Luka Khitaridze
2 years ago
0
PHP
Array
0
// Given an array nums containing n distinct numbers in the range [0, n], // return the only number in the range that is missing from the array.
function missingNumber($nums) {
    sort($nums);
    foreach ($nums as $key => $num) {
        if ($key != $num) {
            return $key;
        }
    }
    return count($nums);
}
echo missingNumber([0, 3, 1]); // 2 echo missingNumber([0, 1]); // 2
by Luka Khitaridze
2 years ago
0
PHP
Array
0
Valid Parentheses CODE
// Determine whether parentheses are valid if they contain some kind of mathematical operations or just text
function isValid($string) {
    $len = strlen($string);
    $filtered = '';
    for ($x = 0;  $x < $len;  $x++) {
	$ASCII = ord($string[$x]);
	if (($ASCII==91 || $ASCII==93 || $ASCII==123 || $ASCII==125 || $ASCII==40 || $ASCII==41)) {
		$filtered .= $string[$x];
	}
    }
    $len2 = strlen($filtered);
    if($len2%2==1){
    return false;
    }
    $parentheses = [
	"("=>")", 
	"["=>"]" , 
	"{"=>"}",
    ];
    $open = [];
    for ($x = 0;  $x < $len2;  $x++) {
	if (array_key_exists($filtered[$x], $parentheses)) {
		array_push($open, $filtered[$x]);
	} else {
		$parenthesis = array_pop($open);
		if ($filtered[$x] != $parentheses[$parenthesis]) {
			return false;
		}
	}
    }
    if ($open) {
	return false;
    }
    return true;
}
echo isValid("{ :'> baeknwaw adanww a} [ daw  ] awda( :.a,a)a"); // returns true
echo isValid("(1 + 1) = 2"); // returns true
echo isValid("(1 + 1)) = 2"); // returns false
// enjoy with coding))
by Luka Khitaridze
2 years ago
0
PHP
Syntax
0
// Reverse bits of a given 32 bits. // The input must be a binary string of length 32 // Example : input: n = 00000010100101000001111010011100, Output: 964176192 (00111001011110000010100101000000) - reversed.
function reverseBits($n) {
    $n = bindec(strrev($n));
    return $n;
}
echo reverseBits("00000010100101000001111010011100");
// P.S onlinephp returns correct answer: 964176192, but
// leet code returns 1 at the same case,
// can someone explain to me in the comments ? 
by Luka Khitaridze
2 years ago
0
PHP
String
1
// Write a function that reverses a string. The input string is given as an array of characters. // Example: Input: s = ["h","e","l","l","o"], Output: ["o","l","l","e","h"]
// with built-in function
function reverseString($s) {
    $s = array_reverse($s);
    return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
// with loop
function reverseString($s) {
    $count = count($s);
    $array = [];
    for ($i = 0; $i < $count; $i++) {
	array_unshift($array, $s[$i]); // inserts element at the beginning of the array
    }
    $s = $array;
    return $s;
}
print_r(reverseString(["h", "e" , "l", "l", "o"]));
by Luka Khitaridze
2 years ago
0
PHP
Array
1
// Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
function thirdMax($nums) {
    $nums = array_unique($nums);  // built-in function that removes duplicated values in array
    sort($nums);
    $count = count($nums);
    if ($count >= 3) {
	array_pop($nums);
	array_pop($nums);
    }
    return end($nums); // built-in function that returns the last element of array
}
echo thirdMax([2, 2, 3, 1]);
by Luka Khitaridze
2 years ago
0
PHP
Array
0
Results: 1578