PHP Classes

File: examples/DifferencesFunctions.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Mo´se  >  PHP Dictionary to Array class  >  examples/DifferencesFunctions.php  >  Download  
File: examples/DifferencesFunctions.php
Role: Example script
Content type: text/plain
Description: example script
Class: PHP Dictionary to Array class
Manipulate value collections as arrays or objects
Author: By
Last change:
Date: 3 years ago
Size: 6,832 bytes
 

Contents

Class file image Download
with an xDict object no matter the data your xDict object contains you can use Differences functions<br>
normally without stress of invalid data types errors.Of course for the methods<br>
which allow user defined callback you can use any type of callback(closure,normal function,classes non <br>static methods
or static methods as stated in the PHP doc.<br>
for the highlighted following sources you got the result below each source:<br><?php
require_once('./../xdict.class.php');
echo
'<pre>';
highlight_string('<?php
 $x=xdict(0,\'\',false);
$x->fill_with(array("a" => "vert", "b" => "marron", "c" => "bleu", "rouge"));
$array2 = array("a" => "vert", "jaune", "rouge");
$result =$x->diff_assoc($array2);
print_r($result);
 ?>'
);
$x=xdict(0,'',false);
$x->fill_with(array("a" => "vert", "b" => "marron", "c" => "bleu", "rouge"));
$array2 = array("a" => "vert", "jaune", "rouge");
$result =$x->diff_assoc($array2);
print_r($result);

highlight_string('<?php
$x=xdict(0,\'\',false);
$x->fill_with(array(\'blue\' => 1, \'red\' => 2, \'green\' => 3, \'purple\' => 4));
$array2 = array(\'green\' => 5, \'blue\' => 6, \'yellow\' => 7, \'cyan\' => 8);

print_r($x->diff_key($array2));
?>'
);
$x=xdict(0,'',false);
$x->fill_with(array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4));
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);

print_r($x->diff_key($array2));

highlight_string('<?php
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}

$x=xdict(0,\'\',false);
$x->fill_with(array("a" => "vert", "b" => "marron", "c" => "bleu", "rouge"));
$array2 = array("a" => "vert", "jaune", "rouge");
$result = $x->diff_uassoc($array2, "key_compare_func");
print_r($result);
?>'
);
function
key_compare_func($a, $b)
{
    if (
$a === $b) {
        return
0;
    }
    return (
$a > $b)? 1:-1;
}

$x=xdict(0,'',false);
$x->fill_with(array("a" => "vert", "b" => "marron", "c" => "bleu", "rouge"));
$array2 = array("a" => "vert", "jaune", "rouge");
$result = $x->diff_uassoc($array2, "key_compare_func");
print_r($result);


highlight_string('<?php
function key_compare_func2($key1, $key2)
{
    if ($key1 == $key2)
        return 0;
    else if ($key1 > $key2)
        return 1;
    else
        return -1;
}

$x=xdict(0,\'\',false);
$x->fill_with( array(\'blue\' => 1, \'red\' => 2, \'green\' => 3, \'purple\' => 4));
$array2 = array(\'green\' => 5, \'blue\' => 6, \'yellow\' => 7, \'cyan\' => 8);

print_r($x->diff_ukey($array2, \'key_compare_func2\'));

?>'
);
function
key_compare_func2($key1, $key2)
{
    if (
$key1 == $key2)
        return
0;
    else if (
$key1 > $key2)
        return
1;
    else
        return -
1;
}

$x=xdict(0,'',false);
$x->fill_with( array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4));
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);

print_r($x->diff_ukey($array2, 'key_compare_func2'));


highlight_string('<?php
$x=xdict(0,\'\',false);
$x->fill_with(array("a" => "green", "red", "blue", "red"));
$array2 = array("b" => "green", "yellow", "red");
$result = $x->diff($array2);

print_r($result);
 ?>'
);
$x=xdict(0,'',false);
$x->fill_with(array("a" => "green", "red", "blue", "red"));
$array2 = array("b" => "green", "yellow", "red");
$result = $x->diff($array2);

print_r($result);
highlight_string('<?php
class cr {
    private $priv_member;
    function cr($val)
    {
        $this->priv_member = $val;
    }

    static function comp_func_cr($a, $b)
    {
        if ($a->priv_member === $b->priv_member) return 0;
        return ($a->priv_member > $b->priv_member)? 1:-1;
    }
   
    static function comp_func_key($a, $b)
    {
        if ($a === $b) return 0;
        return ($a > $b)? 1:-1;
    }
}

$x=xdict(0,\'\',false);
$x->fill_with(array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),));
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result =$x->udiff_assoc($b, array("cr", "comp_func_cr"));
print_r($result);
$result =$x->udiff_uassoc($b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
print_r($result);

?>'
);
class
cr {
    private
$priv_member;
    function
cr($val)
    {
       
$this->priv_member = $val;
    }

    static function
comp_func_cr($a, $b)
    {
        if (
$a->priv_member === $b->priv_member) return 0;
        return (
$a->priv_member > $b->priv_member)? 1:-1;
    }
   
    static function
comp_func_key($a, $b)
    {
        if (
$a === $b) return 0;
        return (
$a > $b)? 1:-1;
    }
}

$x=xdict(0,'',false);
$x->fill_with(array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1=> new cr(4), 2 => new cr(-15),));
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1=> new cr(4), 2 => new cr(-15),);

$result =$x->udiff_assoc($b, array("cr", "comp_func_cr"));
print_r($result);
$result =$x->udiff_uassoc($b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));
print_r($result);

highlight_string('<?php
$x=xdict(0,\'\',false);
$x->fill_with(array(new stdclass, new stdclass,
                new stdclass, new stdclass,
               ));

$array2 = array(
                new stdclass, new stdclass,
               );


$x[0]->width = 11; $x[0]->height = 3;
$x[1]->width = 7; $x[1]->height = 1;
$x[2]->width = 2; $x[2]->height = 9;
$x[3]->width = 5; $x[3]->height = 7;

$array2[0]->width = 7; $array2[0]->height = 5;
$array2[1]->width = 9; $array2[1]->height = 2;

function compare_by_area($a, $b) {
    $areaA = $a->width * $a->height;
    $areaB = $b->width * $b->height;
   
    if ($areaA < $areaB) {
        return -1;
    } elseif ($areaA > $areaB) {
        return 1;
    } else {
        return 0;
    }
}

print_r($x->udiff( $array2, \'compare_by_area\'));
?>'
);


$x=xdict(0,'',false);
$x->fill_with(array(new stdclass, new stdclass,
                new
stdclass, new stdclass,
               ));

$array2 = array(
                new
stdclass, new stdclass,
               );


$x[0]->width = 11; $x[0]->height = 3;
$x[1]->width = 7; $x[1]->height = 1;
$x[2]->width = 2; $x[2]->height = 9;
$x[3]->width = 5; $x[3]->height = 7;

$array2[0]->width = 7; $array2[0]->height = 5;
$array2[1]->width = 9; $array2[1]->height = 2;

function
compare_by_area($a, $b) {
   
$areaA = $a->width * $a->height;
   
$areaB = $b->width * $b->height;
   
    if (
$areaA < $areaB) {
        return -
1;
    } elseif (
$areaA > $areaB) {
        return
1;
    } else {
        return
0;
    }
}

print_r($x->udiff( $array2, 'compare_by_area'));
?>

For more information send a message to info at phpclasses dot org.