1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| <?php
/*
* This is the XXX Project Licence
* do What The Fuck you want to Public License
*
* Version 1.0, March 2000
* Copyright (C) 2000 Banlu Kemiyatorn (]d).
* 136 Nives 7 Jangwattana 14 Laksi Bangkok
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* Ok, the purpose of this license is simple
* and you just
*
* DO WHAT THE FUCK YOU WANT TO.
*
*/
/**
* Ling_Function_ArrayMixer.php
*
* @author Ling
* 12 déc. 2009 16:52:36
*
*/
class Ling_Function_ArrayMixer
{
protected $result;
protected $popNext;
protected $matrix;
protected $newArray;
protected $oldArray;
public function __construct()
{
$this->result = array();
$this->popNext = false;
$this->matrix = array();
$this->newArray = array();
$this->oldArray = array();
}
public function append(array $array)
{
$this->matrix[] = $array;
}
public function makeTree()
{
$lastArray = array_pop($this->matrix);
if(count($this->matrix) > 0)
{
foreach(end($this->matrix) as $k => $v)
{
$this->newArray[$v] = $lastArray;
}
while(count($this->matrix) > 1)
{
$lastArray = array_pop($this->matrix);
$this->oldArray = $this->newArray;
$this->newArray = array();
foreach(end($this->matrix) as $k => $v)
{
$this->newArray[$v] = $this->oldArray;
}
}
}
else
{
$this->result = $lastArray;
}
}
public function ListageArray($tb, $todisplay=array())
{
foreach($cit = new CachingIterator(new ArrayIterator($tb)) as $key => $value)
{
if(is_array($value))
{
if($this->popNext === true)
{
array_pop($todisplay);
$this->popNext = false;
}
$todisplay[] = $key;
self::ListageArray($value, $todisplay);
}
else
{
$prefix = implode("_",$todisplay) . "_";
$this->result[] = $prefix . $value;
if(!$cit->hasNext())
{
$this->popNext = true;
}
}
}
}
public function proceed()
{
$this->makeTree();
$this->ListageArray($this->newArray);
}
public function result()
{
return $this->result;
}
}
//$t1 = array('a','b','c');
//$t2 = array('m1','m2','m3','haha','oups');
//$t3 = array('ok','ko');
//$t4 = array('poule','merci','yoma');
//
//$x = new Ling_Function_ArrayMixer();
//$x->append($t1);
//$x->append($t2);
//$x->append($t3);
//$x->append($t4);
//$x->proceed();
//a($x->result()); |
Partager