PHP Classes

PHP Two Dimensional Array Collection: Manage value collections in two dimensional arrays

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 57%Total: 276 All time: 7,657 This week: 560Up
Version License PHP version Categories
collection-2d 1.0.0GNU General Publi...5PHP 5, Data types
Description 

Author

This class can manage value collections in two dimensional arrays.

It can perform operations to manipulate a list of array items.

Currently it can add an array of items to the collectiomn, reindex the collection, get the current or the next item, go to the first, last, previous, last, or a specific item by position number, move or remove items, add or update items, sort the items by a criteria, search for items by a regular expression in specific item fields.

Picture of Everton da Rosa
  Performance   Level  
Name: Everton da Rosa <contact>
Classes: 17 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 3x

Example

<?php

/*
 * Copyright (C) 2014 Everton
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Example use of PTK\utils\Collection
 */

try{
    require
'examples.inc.php';
   
   
//creating a collection
   
$mycollection = new \Ptk\utils\Collection(array(
        array(
'name' => 'Homer', 'genre' => 'masc')
        ,array(
'name' => 'Marge', 'genre' => 'fem')
        ,array(
'name' => 'Lisa', 'genre' => 'fem')
        ,array(
'name' => 'Bart', 'genre' => 'masc')
        ,array(
'name' => 'Maggie', 'genre' => 'fem')
    ));
   
    echo
"The current record into collection is:".PHP_EOL;
   
var_dump($mycollection->getCurrent());
   
    echo
PHP_EOL."The next name is: ".$mycollection->getNext()['name'].PHP_EOL;
   
   
//advancing to the next record
   
$mycollection->goNext();
    echo
"The current row now is: ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//back to the previous record
   
$mycollection->goPrevious();
    echo
"The previous record: ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//going to end of collection
   
$mycollection->goLast();
    echo
"The last name is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//going to first record
   
$mycollection->goFirst();
    echo
"The first name is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//go to row #3
   
$mycollection->go(3);
    echo
"The #3 row is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//moving to next 2 positions
   
$mycollection->goFirst();
   
$mycollection->move(2);
    echo
"The name for 2 positions from first is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
$mycollection->goLast();
    
//moving back 1 positions
   
$mycollection->move(-1);
    echo
"The name for 1 positions from last is ".$mycollection->getCurrent()['name'].PHP_EOL;
   
   
//removing elements
   
$mycollection->remove();
   
   
//adding elements
   
$mycollection->add(array('name' => 'Milhouse', 'genre' => 'fem'));
   
   
//editing element
   
$mycollection->goLast();
   
$mycollection->update(array('name' => 'Milhouse', 'genre' => 'masc'));
   
   
//sorting data
   
$mycollection->sort('genre', SORT_ASC, 'name', SORT_DESC);
    echo
'<pre>';
   
var_dump($mycollection->fetchArray());
    echo
'</pre>';
   
   
$collection = $mycollection->seek('[M*]', array('name'));
    echo
'<pre>';
   
var_dump($collection->fetchObject());
    echo
'</pre>';
} catch (
Exception $ex) {
    echo
$ex->getMessage();
    exit(
$ex->getCode());
}



  Files folder image Files (5)  
File Role Description
Accessible without login Plain text file collection.php Example Example of use
Accessible without login Plain text file examples.inc.php Aux. A required file to example
Plain text file ptk.class.php Class Auxiliary file
Accessible without login Plain text file ptk.inc.php Aux. Auxiliary file
Plain text file ptk.utils.collection.class.php Class Main class file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:276
This week:0
All time:7,657
This week:560Up
 User Ratings  
 
 All time
Utility:83%StarStarStarStarStar
Consistency:66%StarStarStarStar
Documentation:-
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:57%StarStarStar
Rank:1699