PHP Classes

PHP Class Data Structures: Manipulate data structures using 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
Not enough user ratingsTotal: 74 All time: 10,213 This week: 206Up
Version License PHP version Categories
dshandler 1.0.0MIT/X Consortium ...5PHP 5, Data types
Description 

Author

This class can manipulate data structures using arrays.

It can an array that contains associative arrays of data and can perform several types of operations. Currently it can:

- Sort the array based on the values of one of the keys with a given name.
- Remove duplicate array entries that have the same value for a given key.
- Remove arrays entries with keys that have a given value.

Picture of Muhammad Umer Farooq
Name: Muhammad Umer Farooq is available for providing paid consulting. Contact Muhammad Umer Farooq .
Classes: 52 packages by
Country: Pakistan Pakistan
Age: 23
All time rank: 84310 in Pakistan Pakistan
Week rank: 44 Up1 in Pakistan Pakistan Up
Innovation award
Innovation award
Nominee: 6x

Example

<?php

require_once "../vendor/autoload.php";

$handler = new Lablnet\DSHandler();

$products = [
    [
       
'id' => 1,
       
'name' => 'Test 1',
       
'price' => 300,
       
'rating' => 4.5
   
],
    [
       
'id' => 2,
       
'name' => 'Test 2',
       
'price' => 600,
       
'rating' => 4.1
   
],
    [
       
'id' => 3,
       
'name' => 'Test 3',
       
'price' => 150,
       
'rating' => 3.1
   
],
    [
       
'id' => 4,
       
'name' => 'Test 4',
       
'price' => 700,
       
'rating' => 5
   
],
    [
       
'id' => 5,
       
'name' => 'Test 5',
       
'price' => 900,
       
'rating' => 2.9
   
],
    [
       
'id' => 6,
       
'name' => 'Test 5',
       
'price' => 265,
       
'rating' => 2.9
   
]
];

//Now sort the products base on price
$sort_1_price = $products;
$handler->_sort($sort_1_price, 'price', SORT_ASC);
echo
"<h3>Sort by price ASC</h3><pre>";
print_r($sort_1_price);
echo
"</pre>";
echo
"<br>";
$sort_2_price = $products;
$handler->_sort($sort_2_price, 'price', SORT_DESC);
echo
"<h3>Sort by price DESC</h3><pre>";
print_r($sort_2_price);
echo
"</pre>";
echo
"<br>";

//Now sort the product base on rating
$sort_1_rating = $products;
$handler->_sort($sort_1_rating, 'rating', SORT_ASC);
echo
"<h3>Sort by Rating ASC</h3><pre>";
print_r($sort_1_rating);
echo
"</pre>";
echo
"<br>";
$sort_2_rating = $products;
$handler->_sort($sort_2_rating, 'rating', SORT_DESC);
echo
"<h3>Sort by Rating DESC</h3><pre>";
print_r($sort_2_rating);
echo
"</pre>";
echo
"<br>";

//remove duplicate array by key
$unique = $handler->_unique($products, 'name');
echo
"<h3>Remove duplicate value by key</h3><pre>";
print_r($unique);
echo
"</pre>";
echo
"<br>";

//remove array by key:value
$handler->_remove($products, 'rating', 2.9);
echo
"<h3>Remove array by key:value</h3><pre>";
print_r($products);
echo
"</pre>";
echo
"<br>";


Details

PHP DataStructure Handler

This package can manipulate data structures.

Requirement

  1. PHP
  2. Composer

Installation

Installing this package is very simple, first ensure you have the right PHP version and composer installed then in your terminal/(command prompt) run: composer require lablnet/dshandler

Feature

1. Sort multi dimessional array based on column. 2. Remove duplicate array base on key 3. Remove array, base on key:value pair.

## TODO [ ] Allow to specificy range on remove array:

 - Like we should able to say, remove products below rating 3...

Usage

Consider the following dataset

$products = [
	[
		'id' => 1,
		'name' => 'Test 1',
		'price' => 300,
		'rating' => 4.5
	],
	[
		'id' => 2,
		'name' => 'Test 2',
		'price' => 600,
		'rating' => 4.1
	],
	[
		'id' => 3,
		'name' => 'Test 3',
		'price' => 150,
		'rating' => 3.1
	],
	[
		'id' => 4,
		'name' => 'Test 4',
		'price' => 700,
		'rating' => 5
	],
	[
		'id' => 5,
		'name' => 'Test 5',
		'price' => 900,
		'rating' => 2.9
	],
	[
		'id' => 6,
		'name' => 'Test 5',
		'price' => 265,
		'rating' => 2.9
	]
];

Initialise the classs

require_once "../vendor/autoload.php";

$handler = new Lablnet\DSHandler();

  1. Remove array/products by key:value
    //remove array by key:value
    $handler->_remove($products, 'rating', 2.9);
    echo "<h3>Remove array by key:value</h3><pre>";
    print_r($products);
    echo "</pre>";
    echo "<br>";
    
  2. Remove duplicate array by key
    //remove duplicate array by key
    $unique = $handler->_unique($products, 'name');
    echo "<h3>Remove duplicate value by key</h3><pre>";
    print_r($unique);
    echo "</pre>";
    echo "<br>";
    
  3. Sort the products base on price
    //Now sort the products base on price
    $sort_1_price = $products;
    $handler->_sort($sort_1_price, 'price', SORT_ASC);
    echo "<h3>Sort by price ASC</h3><pre>";
    print_r($sort_1_price);
    echo "</pre>";
    echo "<br>";
    $sort_2_price = $products;
    $handler->_sort($sort_2_price, 'price', SORT_DESC);
    echo "<h3>Sort by price DESC</h3><pre>";
    print_r($sort_2_price);
    echo "</pre>";
    echo "<br>";
    
  4. Sort the products based on rating
    //Now sort the product base on rating
    $sort_1_rating = $products;
    $handler->_sort($sort_1_rating, 'rating', SORT_ASC);
    echo "<h3>Sort by Rating ASC</h3><pre>";
    print_r($sort_1_rating);
    echo "</pre>";
    echo "<br>";
    $sort_2_rating = $products;
    $handler->_sort($sort_2_rating, 'rating', SORT_DESC);
    echo "<h3>Sort by Rating DESC</h3><pre>";
    print_r($sort_2_rating);
    echo "</pre>";
    echo "<br>";
    

Example

The example file is distrubuted with this package.

License

This package is open-sourced software licensed under the MIT license.

Questions

If you have any question feel free to ask me

https://www.linkedin.com/in/lablnet/ https://lablnet.github.io/profile


  Files folder image Files (5)  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file readme.md Doc. Documentation

  Files folder image Files (5)  /  example  
File Role Description
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (5)  /  src  
File Role Description
  Plain text file DSHandler.php Class Class source

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  
 100%
Total:74
This week:0
All time:10,213
This week:206Up
User Comments (1)