Virtlab:Řídící server/SupportFunctions.php.inc

Z VirtlabWiki

Přejít na: navigace, hledání

Pomocné funkce.

Popis funkcí

function ClearWhitespaceInXML($data) 
ze vstupních dat (XML dat), odstraní bílé znaky mezi značkami.
function Unique($array) 
z pole udělá množinu (odstraní duplicitní hodnoty)
function array_delete(&$array, $value) 
z pole $array odstraní hodnotu $value.
function array_porovnej($array1, $array2) 
vypočíta rozdíl multimnožin $array1 - $array2
function DoubleArrayItems($array) 
zdvojí prvky pole
function MatrixClear(&$matice, $radek, $sloupec) 
v zadané matici zruší určený sloupec a řádek

Příklady

$xml = "<tag1>Hello World</tag1>
 <tag1>Hello World</tag1>       <tag1>Hello World</tag1>
 <tag1>Hello World</tag1>";

$cisto = ClearWhitespaceInXML($xml);

$cisto == "<tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1><tag1>Hello World</tag1>";
$a1  = array(1, 1, 1, 2, 3, 4, 5, 5, 6, 6, 6, 6);
$a2  = array(1, 1, 1, 1, 2, 4, 5, 6, 6, 6);
$a12 = array_porovnej($a1, $a2);
$a21 = array_porovnej($a2, $a1);

$a1
(
   [0] => 1
   [1] => 1
   [2] => 1
   [3] => 2
   [4] => 3
   [5] => 4
   [6] => 5
   [7] => 5
   [8] => 6
   [9] => 6
   [10] => 6
   [11] => 6
)

$a2
(
   [0] => 1
   [1] => 1
   [2] => 1
   [3] => 1
   [4] => 2
   [5] => 4
   [6] => 5
   [7] => 6
   [8] => 6
   [9] => 6
)

$a12
(
   [4] => 3
   [7] => 5
   [11] => 6
)

$a21
(
   [3] => 1
)
$a = array(1, "index1" => 2, 3, "index2" => 4, "index3" => 5, 
           6 => array(3, "index4" => 4, "index5" => 5));
$a2 = DoubleArrayItems($a);

$a
(
   [0] => 1
   [index1] => 2
   [1] => 3
   [index2] => 4
   [index3] => 5
   [6] => Array
       (
           [0] => 3
           [index4] => 4
           [index5] => 5
       )
)

$a2
(
   [0] => 1
   [1] => 1
   [2] => 2
   [3] => 2
   [4] => 3
   [5] => 3
   [6] => 4
   [7] => 4
   [8] => 5
   [9] => 5
   [10] => Array
       (
           [0] => 3
           [index4] => 4
           [index5] => 5
       )
   [11] => Array
       (
           [0] => 3
           [index4] => 4
           [index5] => 5
       )
)
$a
(
   [radek1] => Array
       (
           [sloupec1] => 0
           [sloupec2] => 1
           [sloupec3] => 2
           [sloupec4] => 3
       )
   [radek2] => Array
       (
           [sloupec1] => 0
           [sloupec3] => 2
           [sloupec4] => 3
       )
   [radek3] => Array
       (
           [sloupec3] => 2
           [sloupec4] => 3
       )
)

MatrixClear(&$a, "radek2", "sloupec1");

$a
(
   [radek1] => Array
       (
           [sloupec2] => 1
           [sloupec3] => 2
           [sloupec4] => 3
       )
   [radek3] => Array
       (
           [sloupec3] => 2
           [sloupec4] => 3
       )
)

Zdrojový kód

<?php

function ClearWhitespaceInXML($data) {
    $pattern = "/>\s+</";
    $replacement = "><";
    return preg_replace($pattern, $replacement, $data);
}//function

function Unique($array) {
    if(is_array($array))
        return array_keys(array_flip($array));
    return NULL;
}//function

function array_delete(&$array, $value) {
    if(is_array($array)) {
        if(in_array($value, $array)) {
            $array = array_diff($array, array($value));
            return 1;
        }
        return 0;
    }//if
    return NULL;
}

function array_porovnej($array1, $array2) {
    $result = array();
    foreach($array1 as $idx1 => $value1) {
        $exists = 0;
        foreach($array2 as $idx2 => $value2) {
            if($value1 == $value2) {
                unset($array2[$idx2]);
                $exists = 1;
                break;
            }
            //else $result[$idx1] = $value1;
        }//foreach
        if(!$exists) $result[$idx1] = $value1;
    }//foreach
    return $result;
}//function

function DoubleArrayItems($array) {
    $temp = array();
    if(is_array($array)) {
        foreach($array as $hodnota) {
            array_push($temp, $hodnota);
            array_push($temp, $hodnota);
        }//foreach
        return $temp;
    }//if
    return NULL;
}//function

function MatrixClear(&$matice, $radek, $sloupec) {
    unset($matice[$radek]);
    foreach($matice as $radecek => $radky)
        foreach($radky as $sloupecek => $hodonota) {
            if($sloupecek == $sloupec) 
                unset($matice[$radecek][$sloupecek]);
    }
}//function
?>

Osobní nástroje