Location: PHPKode > scripts > Collection > collection/Collection.test.php
<?php
include("Collection.class.php");


// We create an array
$a[] = "Diogo";
$a[] = "Souza";
$a[] = "Silva";

echo "We first create the array : " ;
var_dump($a);
// Them we call a collection, with this array
$col1 = new Collection($a);

echo "Now we created the collection : ";
var_dump($col1) ;

// We can create another array to collection
$b[] = "Debora";
$b[] = "Souza";
$col2 = new Collection($b);


echo "Creating other array, and them other collection : ";
var_dump($col2);

$col1->merge($col2); // Them we can join both collections, or an array

echo "Merge the two collections : ";
var_dump($col1);

$col3 = new Collection($col1); // And we can create a collection from another col

echo "Create another collection, giving last collection as start point : ";
var_dump($col3);

$col3->add("Marilice"); // This is how we add itens to the collection
$col3->add("aff","not removed")->add("Mozart"); // We can define the key(index) in the second arg, and chain
echo "Added 'marilice' and them added 'not removed' at index 'aff'";
echo var_dump($col3);

$col3->remove($col3->indexOf("Silva")); // Removing a item, givin the index 
echo "Removed 'silva' by giving it index, them iterate over it";

echo "<ul>" ;
// This is a way to iterate over the array
// We have next, back, forward, rewind that relies on php array manipulation
do {
	echo "<li> (".$col3->currentKey().") ".$col3->current()."</li>";
} while($col3->next()) ;
echo "</ul>";

$new = $col3->subCollection(2,5); // We can get a collection from a collection
$new->add("Debora");

echo "Created new collection, by sub collection from 2 to 5 , and added 'Debora'";
echo var_dump($col3);

echo "Index of (Debora) = ".$new->IndexOf("Debora")." in new <br/>";
echo "Last Index Of (Debora) = ".$new->LastIndexOf("Debora")." in new <br/>";

echo "<ul>" ;
for($i=0;$i<$new->count();$i++) {
	$now = $new->get($i);
	echo "<li>{$i} = {$now}</li>";
}
echo "</ul>";

$test = $new->toArray() ; // To array
echo "new as  array" ;
var_dump($test);

echo "Now we will use a new Collection";
unset($a);
$a[] = "Elemento Um!";
$a[] = "Elemento Dois!";
$a["teste"] = "Elemento Teste";

$col = new Collection($a);

var_dump($col);

echo "Lets see if it implements Iterator correctly, looping on while()";

$col->rewind() ;
echo "<ul>";
while($col->valid()) {
	echo "<li>{$col->key()} = {$col->current()}</li>";
	$col->next();
}
echo "</ul>";

echo "Looping on Foreach";
echo "<ul>";
foreach($col as $k=>$v) {
	echo "<li>$k = $v</li>";
}
echo "</ul>";

echo "Now some pretty cool stuff: Deeper collection, multilevel arrays";
unset($col);
$col = new Collection; // This will be a multilevel collection
$col->set("indexes",new Collection);
$col->set("columns",new Collection);

$col->get("indexes")->append("12")->append("25") ;
$col->get("columns")->append("name")->append("age")->append("email") ;
$col->set("values",new Collection)->append("Diogo")->append("19")->append("hide@address.com");

echo "<br />Collection";
echo "<ul>";
foreach($col as $key=>$val) {
	echo "<li>Collection ".$key ;
	echo "<ul>";
	foreach($val as $k=>$v) {
		echo "<li> $k = $v</li>";
	}
	echo "</ul></li>";
}
echo "</ul>";

echo "And we are done for now! You got also sort, ksort, asort, append, clear, count... a lot of things";

echo "<br /><br/>Brand new Feature! ArrayAccess, beyond foreach and sutff now \$col[1] will work";

echo "<br />Result of \$col[\"indexes\"][0] is ".$col["indexes"][0];
echo "<br />Result of \$col[\"values\"][2] is ".$col["values"][2];

echo "<br><BR>Setting also work:";
echo "<br>We will do: \$col[\"indexes\"][] = \"35\" and \$col[\"values\"][2] = \"hide@address.com\"(old, hide@address.com) ";
 
    $col["indexes"][] = 35 ;
    $col["values"][2] = "hide@address.com" ;
echo "<br />Result of \$col[\"indexes\"][2] is ".$col["indexes"][2];
echo "<br />Result of \$col[\"values\"][2] is ".$col["values"][2];

echo "<br /><br />So much work for a class to behave like a array... but classes are extensible.";
?>
Return current item: Collection