/ Published in: PHP
Sort & query arrays in PHP. Sort multi dimensional arrays by object properties and filter the array by specific criteria.
Documentation:
https://github.com/mattnicholson/DataList
Documentation:
https://github.com/mattnicholson/DataList
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<? /* DataList ---------------------------------------------- Query and Sort multi-dimensional arrays in PHP @author Matt Nicholson <[email protected]> @website https://github.com/mattnicholson/DataList.git */ class DataList{ public $list; public $sortable; public $subset; public $wheres; { $this->sortable = $list; } function where($prop,$rule){ foreach($subset as $item): if($this->match($item,$prop,$rule)) $this->subset[] = $item; endforeach; } function match($item,$prop,$rule){ $this->wheres[] = $rule; $prop = $this->getRawValue($item,$prop); switch($type): case 'string': break; case 'object': $value = $prop->getTimestamp(); else: $value = 0; endif; break; case 'integer' : $value = $prop; break; default: $match = 0; break; endswitch; return $match; } function find(){ return $this->sortable; } function all(){ return $this->sortable; } function length(){ } $i = 0; // Allow a string to be supplied foreach($this->sortable as $item): $k = $this->makeSortKey($item,$props); // Add a unique counter to the end $unique = $this->padValue($i); $k = $k.$unique; $arr[$k] = $item; $i++; endforeach; switch($order): case 'DESC': break; default: break; endswitch; return $arr; } function makeSortKey($item,$props){ $i = 0; $k = $this->getSortablePropValue($item,$props[0]); else: $k = ""; foreach($props as $prop): $value = $this->getSortablePropValue($item,$prop); $k = $k.$value; $i++; endforeach; endif; return $k; } function getNestedProperty($item,$prop){ $i = $item; foreach($levels as $level): $i = $i->{$level}; endforeach; return $i; } function getRawValue($item,$prop){ $prop = $this->getNestedProperty($item,$prop); else: $prop = $item->{$prop}; endif; if(preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})$/',$prop)) $prop = new DateTime($prop); return $prop; } function getSortablePropValue($item,$prop){ $prop = $this->getRawValue($item,$prop); $dir = STR_PAD_LEFT; $pad = 30; switch($type): case 'string': $dir = STR_PAD_RIGHT; $pad = 100; break; case 'object': $value = $prop->getTimestamp(); else: $value = 0; endif; break; case 'integer': $value = $prop; break; default: $value = 0; break; endswitch; return $this->padValue($value,$pad,$dir); } function getPropValue($item,$prop,$sortable){ $prop = $this->getNestedProperty($item,$prop); else: $prop = $item->{$prop}; endif; $dir = STR_PAD_LEFT; switch($type): case 'string': $dir = STR_PAD_RIGHT; break; case 'object': $value = $prop->getTimestamp(); else: $value = 0; endif; break; case 'integer': $value = $prop; break; default: $value = 0; break; endswitch; return $this->padValue($value,100,$dir); } function padValue($value,$pad=10,$dir = STR_PAD_LEFT){ } } ?>
URL: https://github.com/mattnicholson/DataList