SimpleXMLElement
converts array to XML
$test_array = array (
'bla' => 'blub',
'foo' => 'bar',
'another_array' => array (
'stack' => 'overflow',
),
);
$xml = new SimpleXMLElement('<rootTag/>');
to_xml($xml, $test_array);
print $xml->asXML();
function to_xml(SimpleXMLElement $object, array $data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$new_object = $object->addChild($key);
to_xml($new_object, $value);
} else {
// if the key is an integer, it needs text with it to actually work.
if ($key != 0 && $key == (int) $key) {
$key = "key_$key";
}
$object->addChild($key, $value);
}
}
}
print_r(fizzBuzz_2(35));
function fizzBuzz_1($n){
$result = [];
for ($i = 1; $i <= $n; $i++) {
$output = "";
if ($i%3 == 0 && $i%5 == 0) {
$output = "FizzBuzz";
}if ($i%3 == 0) {
$output = "Fizz";
}if ($i%5 == 0) {
$output = "Buzz";
}if (!$output) {
$output = $i;
}
$result[] = $output;
}
}
// The solution is faster
function fizzBuzz_2($n) {
$result = [];
for ($i = 1; $i <= $n; $i++) {
if ($i%15 == 0) {
$output = "FizzBuzz";
} else if ($i%3 == 0) {
$output = "Fizz";
} else if ($i%5 == 0) {
$output = "Buzz";
} else {
$output = "$i";
}
$result[] = $output;
}
return $result;
}
<?php
print_r(removeDuplicate_1([2,4,7,5,7,4,1]))."\n";
print_r(removeDuplicate_1([3,5,0,0,1,2,6,6]))."\n";
//Using array_unique function
function removeDuplicate_1($nums){
return array_unique($nums);
}
function removeDuplicates_2($nums){
$nums=[];
foreach ($nums as $num){
if (!in_array($num,$result)){
$result[]=$num;
}
}
return $result;
}
<?php
//moving zeros to the enda and remainig orders of remaining elements
//input:[0,1,0,3,12]=>output:[1,3,12,0,0]
function moveZeroes(&$nums) {
print_r($nums);
$count = count($nums); //counting array elements
for ($i = 0; $i < $count; $i++) {
//counting loops while situated in array
if ($nums[$i] == 0) {
//checking if number is 0
unset($nums[$i]); //unsetting this number and moving to the end
$nums[] = 0;
}
}
}
$array = [0, 1, 0, 3, 12];
moveZeroes($array);
print_r($array);
<?php
var_dump(isOrthogonal([1, 2], [2, -1]))."\n";
var_dump(isOrthogonal([3, -1], [7, 5]))."\n";
var_dump(isOrthogonal([1, 2, 0], [2, -1, 10]))."\n";
function isOrthogonal($arr1, $arr2) {
$prod = 0;
$count = count($arr1);
for ($i = 0; $i < $count; $i++) {
$prod += $arr1[$i]*$arr2[$i];
}
return $prod == 0? true:false;
}
/*
//JS solution:
function isOrthogonal(ar1, arr2){
let prod=0;
let count=arr1.length;
for (i=0; i<count; i++){
prod+=arr1[i]*arr2[i];
}
return prod==0? true; false;
}
*/
<?php
echo discount(5000, 35)."\n";
echo discount(2000, 50)."\n";
echo discount(1070, 10)."\n";
echo discount(670, 3)."\n";
function discount($price, $discount){
return $price-$price/100*$discount;
}
echo bcsqrt('2', 4)."\n"; //1.4142
$return=bcsqrt("81");
echo $return."\n";//კვ.ფესვი 9
//გამოკლება
$a='3.45';
$b='4';
echo bcsub($a,$b)."\n";
echo bcsub($a,$b, 6)."\n";
<?php
echo romanToInt('III')."\n";
echo romanToInt('LVIII')."\n";
echo romanToInt('MCMXCIV')."\n";
/*
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
*/
// Converts the roman numeral to an integer
function romanToInt($roman) {
$res = $next = 0;
// Letters with their value
$numbers = ['I'=>1, 'V'=>5, 'X'=>10, 'L'=>50, 'C'=>100, 'D'=>500, 'M'=>1000];
$len = strlen($roman);
// Loops through letters
for ($i = $len-1; $i >= 0; $i--) {
$num = $numbers[$roman[$i]];
// Subtracts if the left side letter is less
if ($num < $next) {
$res -= $num;
} else {
$res += $num;
}
$next = $num;
}
return $res;
}
<?php
var_dump(containsDuplicate([1,1,2,5,3]));
var_dump(containsduplicate([2,7,7,6,9,0,0]));
var_dump(containsDuplicate([1,4,5,0,7]));
//Using array_count_values
function containsDuplicate($array){
$counted=array_count_values($array);
return max($counted)>1;
}
//Using foreach loop
function containsDuplicate_2($array){
$values=[];
foreach ($array as $item){
if (array_count_values($array)>1){
return true;
}
}
return false;
}
//both works well !!<?php
echo strangeCounter(4)."\n";
echo strangeCounter(10)."\n";
echo strangeCounter(21)."\n";
function strangeCounter($t){
$scnt=3;
// Loops until $scnt is less than $t+2
while ($t+2>=$scnt){
$scnt*=2;
}
// The sum of the number pairs inside the block is
// always the same. The sum is less by 2 than 2*$scnt
return $scnt-$t-2;
}