PHP Classes

File: examples/MapReduceReplaceWalk.php

Recommend this page to a friend!
  Classes of zinsou A.A.E.Moïse   PHP Dictionary to Array class   examples/MapReduceReplaceWalk.php   Download  
File: examples/MapReduceReplaceWalk.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: 6 years ago
Size: 4,247 bytes
 

Contents

Class file image Download
<?php
require_once('./../xdict.class.php');
echo
'<pre>';
highlight_string('<?php
function cube($n)
{
    return($n * $n * $n);
}

$x=xdict(0,"",false);
$x->fill_with(array(1, 2, 3, 4, 5));
$b = $x->map("cube");
print_r($b);

$func = function($value) {
    return $value * 2;
};
$x=xdict(0,"",false);
$x->fill_with(range(1, 5));
print_r($x->map($func));
?>'
);

function
cube($n)
{
    return(
$n * $n * $n);
}


$x=xdict(0,"",false);
$x->fill_with(array(1, 2, 3, 4, 5));
$b = $x->map("cube");
print_r($b);


$func = function($value) {
    return
$value * 2;
};
$x=xdict(0,"",false);
$x->fill_with(range(1, 5));
print_r($x->map($func));
$x=xdict(0,"",false);
$x->fill_with(array(1, 2, 3, 4, 5));
$b = array("one", "two", "three", "four", "five");
$c = array("uno", "dos", "tres", "cuatro", "cinco");

$d = $x->map(null,$b, $c);
print_r($d);

highlight_string('<?php
function sum($carry, $item)
{
    $carry += $item;
    return $carry;
}

function product($carry, $item)
{
    $carry *= $item;
    return $carry;
}

$x=xdict(0,"",false);
$x->fill_with(array(1, 2, 3, 4, 5));
$y = xdict(0,"",false);


var_dump($x->reduce("sum"));
var_dump($x->reduce("product", 10));
var_dump($y->reduce("sum", "No data to reduce"));
?>'
);
function
sum($carry, $item)
{
   
$carry += $item;
    return
$carry;
}

function
product($carry, $item)
{
   
$carry *= $item;
    return
$carry;
}

$x=xdict(0,"",false);
$x->fill_with(array(1, 2, 3, 4, 5));
$y = xdict(0,"",false);



var_dump($x->reduce("sum"));
var_dump($x->reduce("product", 10));
var_dump($y->reduce("sum", "No data to reduce"));
highlight_string('<?php
$x=xdict(0,"",false);
$x->fill_with(array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"));

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2<br />\n";
}

echo "Avant ...:\n";
$x->walk(\'test_print\');

$x->walk(\'test_alter\', \'fruit\');
echo "... et après :\n";

$x->walk(\'test_print\');


$sweet = array(\'a\' => \'apple\', \'b\' => \'banana\');
$x=xdict(0,"",false);
$x->fill_with(array(\'sweet\' => $sweet, \'sour\' => \'lemon\'));

function test_print2($item, $key)
{
    echo "La clé $key contient l\'élément $item\n";
}

$x->walk_recursive(\'test_print2\');
?>'
);
$x=xdict(0,"",false);
$x->fill_with(array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"));

function
test_alter(&$item1, $key, $prefix)
{
   
$item1 = "$prefix: $item1";
}

function
test_print($item2, $key)
{
    echo
"$key. $item2<br />\n";
}

echo
"Avant ...:\n";
$x->walk('test_print');

$x->walk('test_alter', 'fruit');
echo
"... et après :\n";

$x->walk('test_print');


$sweet = array('a' => 'apple', 'b' => 'banana');
$x=xdict(0,"",false);
$x->fill_with(array('sweet' => $sweet, 'sour' => 'lemon'));

function
test_print2($item, $key)
{
    echo
"La clé $key contient l'élément $item\n";
}

$x->walk_recursive('test_print2');


highlight_string('<?php
$x=xdict(0,"",false);
$x->fill_with(array("orange", "banana", "apple", "raspberry"));
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = $x->replace($replacements, $replacements2);
print_r($basket);

$x=xdict(0,"",false);
$x->fill_with(array(\'citrus\' => array( "orange") , \'berries\' => array("blackberry", "raspberry"), ));
$replacements = array(\'citrus\' => array(\'pineapple\'), \'berries\' => array(\'blueberry\'));

$basket = $x->replace_recursive($replacements);
print_r($basket);
?>'
);
$x=xdict(0,"",false);
$x->fill_with(array("orange", "banana", "apple", "raspberry"));
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");

$basket = $x->replace($replacements, $replacements2);
print_r($basket);

$x=xdict(0,"",false);
$x->fill_with(array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), ));
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));

$basket = $x->replace_recursive($replacements);
print_r($basket);

?>