PHP Classes

PHP Order Array Trait: Sort arrays of indexed strings with custom sorting

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 yet rated by the usersTotal: 144 All time: 9,163 This week: 560Up
Version License PHP version Categories
ordertrait 1.0.0The PHP License5.3.0PHP 5, Data types, Traits
Description 

Author

This package can be used to sort arrays of indexed strings with custom sorting.

It provides a trait that can be used directly to sort arrays with members that are associative arrays.

The trait takes a string parameter that defines the name of the array key that will be used to sort the elements.

It returns an array sorted based on character order. The sorting order can be changed using a string that defines the order of the characters in the sorting key element.

Innovation Award
PHP Programming Innovation award winner
May 2020
Winner


Prize: One official elePHPant Plush Mascott
Many applications need to sort information according to criteria. Often that information is stored in associative arrays.

This package provides a trait that can be used to sort any associative array.

It uses a given string for defining the sorting criteria of string values of a given key of the associative array to be sorted.

Manuel Lemos
Picture of Raphael Paez
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

 

Example

<?php

require_once "OrderTrait.php";

$exampleSpl = SplFixedArray::fromArray([
    [
       
"id" => 'arogtrg879512gggfg',
       
"name" => '?aaaaaa',
    ],
    [
       
"id" => 'assdfdsfsddfg8456g',
       
"name" => 'Aaaaaaa',
    ],
    [
       
"id" => 'asdsadfasd68f7sd5f41',
       
"name" => '-A',
    ],
    [
       
"id" => 'asdsadfasd68f7sd5f41',
       
"name" => 'ffffFFff',
    ],
    [
       
"id" => 'argswefweftrg879512gggfg',
       
"name" => 'Ddddddd',
    ]
]);

$array = OrderTrait::orderArrayPerson($exampleSpl->toArray(), 'name');
var_dump($array);


  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file index.php Example Example
Plain text file OrderTrait.php Class Function orderArrayPerson(arrayOrder, attributeNameOrder)

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:144
This week:0
All time:9,163
This week:560Up