Skip to content

Commit 409720e

Browse files
2.4
1 parent e9d7bce commit 409720e

File tree

8 files changed

+275
-67
lines changed

8 files changed

+275
-67
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ArrayOne
2-
It is a minimalist library that process arrays in PHP.
2+
It is a minimalist library that process arrays in PHP with no dependency.
33

44
This library is focused to work with business data(reading/saving files, database records, API, etc.),
55
so it is not similar to Numpy, Pandas, NumPHP or alike because they target difference objectives.
@@ -17,7 +17,7 @@ What it does? Filter, order, renaming column, grouping, validating, amongst many
1717
[![Total Downloads](https://poser.pugx.org/eftec/ArrayOne/downloads)](https://packagist.org/packages/eftec/ArrayOne)
1818
[![Maintenance](https://img.shields.io/maintenance/yes/2024.svg)]()
1919
[![composer](https://img.shields.io/badge/composer-%3E1.6-blue.svg)]()
20-
[![php](https://img.shields.io/badge/php-7.1-green.svg)]()
20+
[![php](https://img.shields.io/badge/php-7.4-green.svg)]()
2121
[![php](https://img.shields.io/badge/php-8.x-green.svg)]()
2222
[![CocoaPods](https://img.shields.io/badge/docs-70%25-yellow.svg)]()
2323

@@ -663,6 +663,10 @@ $this->makeRequestArrayByExample(['a'=1,'b'=>2]); // ['a'='post','b'=>'post'];
663663

664664

665665
## versions
666+
* 2.4 2024-08-15
667+
* [update] sort() now allow to group by multiples columns
668+
* [fixed] group() fixed the value returned. If multiples columns are used then, the new grouping column could be split.
669+
* [new] splitColumn() split a column in two or more columns.
666670
* 2.3 2024-08-10
667671
* [new] sum(),min(),max(),avg(),count(),aggr()
668672
* 2.2 2024-08-06

examples/example1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
->removeCol(['price'])
2121
->col('name')
2222
->last()
23-
->getCurrent();
23+
->all();
2424
echo "<h1>Example1 array:</h1>";
2525
var_dump2($array);
2626
echo "<h1>nav to product, creating a new column, filtering, removing column price, returning a column and getting the last value</h1>";

examples/example2.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
include __DIR__.'/libexample.php';
6565

6666
echo "<h1>Test2 array</h1>";
67-
var_dump2($top);
67+
echo generateTable($top);
6868
echo "<h1>navigation to topping</h1>";
69-
$result= ArrayOne::set($top)->nav('topping')->getCurrent();
70-
var_dump2($result);
69+
$result= ArrayOne::set($top['topping'])->all();
70+
echo generateTable($result);
7171
echo "<h1>And reducing the values (counting)</h1>";
72-
$result= (new ArrayOne($top))->nav('topping')->reduce(['id'=>'count'])->getCurrent();
73-
var_dump2($result);
72+
$result= (new ArrayOne($top['topping']))->reduce(['id'=>'count'])->all();
73+
echo generateTable($result);
7474

examples/example3.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
use eftec\ArrayOne;
3+
4+
5+
include __DIR__.'/../vendor/autoload.php';
6+
include __DIR__.'/libexample.php';
7+
include __DIR__.'/exampledata.php';
8+
echo "<h2>Filter by Office = Tokyo and age>=33. Order by name desc</h2>\n";
9+
echo generateTable(ArrayOne::set($data)
10+
->filter(['Office'=>'eq;Tokyo','Age'=>'ge;33'])
11+
->sort('Name','desc')
12+
->all());
13+
echo "<h2>Order by name and position desc</h2>\n";
14+
echo generateTable(ArrayOne::set($data)
15+
->sort(['Name','Position'],['desc','desc'])
16+
->all());
17+
echo "<h2>Grouped by office and position (index grouping)</h2>\n";
18+
echo generateTable(ArrayOne::set($data)
19+
->group(['Office','Position'],['Salary'=>'sum','Age'=>'avg'])
20+
->all());
21+
echo "<h2>Grouped by office and position (column grouping)</h2>\n";
22+
echo generateTable(ArrayOne::set($data)
23+
->group(['Office','Position'],['Salary'=>'sum','Age'=>'avg'],false)
24+
->all());

examples/exampleFindandFilter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
var_dump2(ArrayOne::isIndexTableArray($values));
1919

2020
echo "<h1>returning all the fruits with price greater or equals than 200</h1>";
21-
var_dump2(ArrayOne::set($values)->filter([['type'=>'eq;fruit'],['price'=>'ge;200']])->getCurrent());
21+
var_dump2(ArrayOne::set($values)->filter([['type'=>'eq;fruit'],['price'=>'ge;200']])->all());
2222
echo "<h1>returning all the fruits with price greater or equals than 200 using a function<h1></h1>";
2323
var_dump2(ArrayOne::set($values)->filter(static function($row) {
2424
return $row['type']==='fruit' && $row['price']>=200;
25-
})->getCurrent());
25+
})->all());
2626
echo "<h1>returning all the fruits with price greater or equals than 200 using a function (lambda)<h1></h1>";
27-
var_dump2(ArrayOne::set($values)->filter(fn($row) => $row['type']==='fruit' && $row['price']>=200)->getCurrent());
27+
var_dump2(ArrayOne::set($values)->filter(fn($row) => $row['type']==='fruit' && $row['price']>=200)->all());
2828

2929
echo "<h1>returning all the fruits and drink with price greater or equals than 200</h1>";
30-
var_dump2(ArrayOne::set($values)->filter([['type'=>'in;fruit,drink'],['price'=>'ge;200']])->getCurrent());
30+
var_dump2(ArrayOne::set($values)->filter([['type'=>'in;fruit,drink'],['price'=>'ge;200']])->all());
3131

3232
echo "<h1>returning all the indexes where the condition is located</h1>";
33-
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],false,'key')->getCurrent());
33+
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],false,'key')->all());
3434
echo "<h1>return the first value who matches the condition</h1>";
35-
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],true,'value')->getCurrent());
35+
var_dump2(ArrayOne::set($values)->find([['type'=>'eq;fruit'],['price'=>'ge;200']],true,'value')->all());

examples/exampledata.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
$data = [['Name' => 'Airi Satou', 'Position' => 'Accountant', 'Office' => 'Tokyo', 'Age' => '33', 'Start date' => '39780', 'Salary' => '162.7'],
3+
['Name' => 'Angelica Ramos', 'Position' => 'Chief Executive Officer (CEO)', 'Office' => 'London', 'Age' => '47', 'Start date' => '40095', 'Salary' => '1200.000'],
4+
['Name' => 'Ashton Cox', 'Position' => 'Junior Technical Author', 'Office' => 'San Francisco', 'Age' => '66', 'Start date' => '39825', 'Salary' => '86'],
5+
['Name' => 'Bradley Greer', 'Position' => 'Software Engineer', 'Office' => 'London', 'Age' => '41', 'Start date' => '41195', 'Salary' => '132'],
6+
['Name' => 'Brenden Wagner', 'Position' => 'Software Engineer', 'Office' => 'San Francisco', 'Age' => '28', 'Start date' => '40701', 'Salary' => '206.85'],
7+
['Name' => 'Brielle Williamson', 'Position' => 'Integration Specialist', 'Office' => 'New York', 'Age' => '61', 'Start date' => '41245', 'Salary' => '372'],
8+
['Name' => 'Bruno Nash', 'Position' => 'Software Engineer', 'Office' => 'London', 'Age' => '38', 'Start date' => '40666', 'Salary' => '163.5'],
9+
['Name' => 'Caesar Vance', 'Position' => 'Pre-Sales Support', 'Office' => 'New York', 'Age' => '21', 'Start date' => '40889', 'Salary' => '106.45'],
10+
['Name' => 'Cara Stevens', 'Position' => 'Sales Assistant', 'Office' => 'New York', 'Age' => '46', 'Start date' => '40883', 'Salary' => '145.6'],
11+
['Name' => 'Cedric Kelly', 'Position' => 'Senior Javascript Developer', 'Office' => 'Edinburgh', 'Age' => '22', 'Start date' => '40997', 'Salary' => '433.06'],
12+
['Name' => 'Charde Marshall', 'Position' => 'Regional Director', 'Office' => 'San Francisco', 'Age' => '36', 'Start date' => '39737', 'Salary' => '470.6'],
13+
['Name' => 'Colleen Hurst', 'Position' => 'Javascript Developer', 'Office' => 'San Francisco', 'Age' => '39', 'Start date' => '40071', 'Salary' => '205.5'],
14+
['Name' => 'Dai Rios', 'Position' => 'Personnel Lead', 'Office' => 'Edinburgh', 'Age' => '35', 'Start date' => '41178', 'Salary' => '217.5'],
15+
['Name' => 'Donna Snider', 'Position' => 'Customer Support', 'Office' => 'New York', 'Age' => '27', 'Start date' => '40568', 'Salary' => '112'],
16+
['Name' => 'Doris Wilder', 'Position' => 'Sales Assistant', 'Office' => 'Sydney', 'Age' => '23', 'Start date' => '40441', 'Salary' => '85.6'],
17+
['Name' => 'Finn Camacho', 'Position' => 'Support Engineer', 'Office' => 'San Francisco', 'Age' => '47', 'Start date' => '40001', 'Salary' => '87.5'],
18+
['Name' => 'Fiona Green', 'Position' => 'Chief Operating Officer (COO)', 'Office' => 'San Francisco', 'Age' => '48', 'Start date' => '40248', 'Salary' => '850'],
19+
['Name' => 'Garrett Winters', 'Position' => 'Accountant', 'Office' => 'Tokyo', 'Age' => '63', 'Start date' => '40749', 'Salary' => '170.75'],
20+
['Name' => 'Gavin Cortez', 'Position' => 'Team Leader', 'Office' => 'San Francisco', 'Age' => '22', 'Start date' => '39747', 'Salary' => '235.5'],
21+
['Name' => 'Gavin Joyce', 'Position' => 'Developer', 'Office' => 'Edinburgh', 'Age' => '42', 'Start date' => '40534', 'Salary' => '92.575'],
22+
['Name' => 'Gloria Little', 'Position' => 'Systems Administrator', 'Office' => 'New York', 'Age' => '59', 'Start date' => '39913', 'Salary' => '237.5'],
23+
['Name' => 'Haley Kennedy', 'Position' => 'Senior Marketing Designer', 'Office' => 'London', 'Age' => '43', 'Start date' => '41261', 'Salary' => '313.5'],
24+
['Name' => 'Hermione Butler', 'Position' => 'Regional Director', 'Office' => 'London', 'Age' => '47', 'Start date' => '40623', 'Salary' => '356.25'],
25+
['Name' => 'Herrod Chandler', 'Position' => 'Sales Assistant', 'Office' => 'San Francisco', 'Age' => '59', 'Start date' => '41127', 'Salary' => '137.5'],
26+
['Name' => 'Hope Fuentes', 'Position' => 'Secretary', 'Office' => 'San Francisco', 'Age' => '41', 'Start date' => '40221', 'Salary' => '109.85'],
27+
['Name' => 'Howard Hatfield', 'Position' => 'Office Manager', 'Office' => 'San Francisco', 'Age' => '51', 'Start date' => '39798', 'Salary' => '164.5'],
28+
['Name' => 'Jackson Bradshaw', 'Position' => 'Director', 'Office' => 'New York', 'Age' => '65', 'Start date' => '39717', 'Salary' => '645.75'],
29+
['Name' => 'Jena Gaines', 'Position' => 'Office Manager', 'Office' => 'London', 'Age' => '30', 'Start date' => '39801', 'Salary' => '90.56'],
30+
['Name' => 'Jenette Caldwell', 'Position' => 'Development Lead', 'Office' => 'New York', 'Age' => '30', 'Start date' => '40789', 'Salary' => '345'],
31+
['Name' => 'Jennifer Acosta', 'Position' => 'Junior Javascript Developer', 'Office' => 'Edinburgh', 'Age' => '43', 'Start date' => '41306', 'Salary' => '75.65'],
32+
['Name' => 'Jennifer Chang', 'Position' => 'Regional Director', 'Office' => 'Singapore', 'Age' => '28', 'Start date' => '40496', 'Salary' => '357.65'],
33+
['Name' => 'Jonas Alexander', 'Position' => 'Developer', 'Office' => 'San Francisco', 'Age' => '30', 'Start date' => '40373', 'Salary' => '86.5'],
34+
['Name' => 'Lael Greer', 'Position' => 'Systems Administrator', 'Office' => 'London', 'Age' => '21', 'Start date' => '39871', 'Salary' => '103.5'],
35+
['Name' => 'Martena Mccray', 'Position' => 'Post-Sales support', 'Office' => 'Edinburgh', 'Age' => '46', 'Start date' => '40611', 'Salary' => '324.05'],
36+
['Name' => 'Michael Bruce', 'Position' => 'Javascript Developer', 'Office' => 'Singapore', 'Age' => '29', 'Start date' => '40721', 'Salary' => '183'],
37+
['Name' => 'Michael Silva', 'Position' => 'Marketing Designer', 'Office' => 'London', 'Age' => '66', 'Start date' => '41240', 'Salary' => '198.5'],
38+
['Name' => 'Michelle House', 'Position' => 'Integration Specialist', 'Office' => 'Sydney', 'Age' => '37', 'Start date' => '40696', 'Salary' => '95.4'],
39+
['Name' => 'Olivia Liang', 'Position' => 'Support Engineer', 'Office' => 'Singapore', 'Age' => '64', 'Start date' => '40577', 'Salary' => '234.5'],
40+
['Name' => 'Paul Byrd', 'Position' => 'Chief Financial Officer (CFO)', 'Office' => 'New York', 'Age' => '64', 'Start date' => '40338', 'Salary' => '725'],
41+
['Name' => 'Prescott Bartlett', 'Position' => 'Technical Author', 'Office' => 'London', 'Age' => '27', 'Start date' => '40670', 'Salary' => '145'],
42+
['Name' => 'Quinn Flynn', 'Position' => 'Support Lead', 'Office' => 'Edinburgh', 'Age' => '22', 'Start date' => '41336', 'Salary' => '342'],
43+
['Name' => 'Rhona Davidson', 'Position' => 'Integration Specialist', 'Office' => 'Tokyo', 'Age' => '55', 'Start date' => '40465', 'Salary' => '327.9'],
44+
['Name' => 'Sakura Yamamoto', 'Position' => 'Support Engineer', 'Office' => 'Tokyo', 'Age' => '37', 'Start date' => '40044', 'Salary' => '139.575'],
45+
['Name' => 'Serge Baldwin', 'Position' => 'Data Coordinator', 'Office' => 'Singapore', 'Age' => '64', 'Start date' => '41008', 'Salary' => '138.575'],
46+
['Name' => 'Shad Decker', 'Position' => 'Regional Director', 'Office' => 'Edinburgh', 'Age' => '51', 'Start date' => '39765', 'Salary' => '183'],
47+
['Name' => 'Shou Itou', 'Position' => 'Regional Marketing', 'Office' => 'Tokyo', 'Age' => '20', 'Start date' => '40769', 'Salary' => '163'],
48+
['Name' => 'Sonya Frost', 'Position' => 'Software Engineer', 'Office' => 'Edinburgh', 'Age' => '23', 'Start date' => '39795', 'Salary' => '103.6'],
49+
['Name' => 'Suki Burks', 'Position' => 'Developer', 'Office' => 'London', 'Age' => '53', 'Start date' => '40108', 'Salary' => '114.5'],
50+
['Name' => 'Tatyana Fitzpatrick', 'Position' => 'Regional Director', 'Office' => 'London', 'Age' => '19', 'Start date' => '40254', 'Salary' => '385.75'],
51+
['Name' => 'Thor Walton', 'Position' => 'Developer', 'Office' => 'New York', 'Age' => '61', 'Start date' => '41497', 'Salary' => '98.54'],
52+
['Name' => 'Tiger Nixon', 'Position' => 'System Architect', 'Office' => 'Edinburgh', 'Age' => '61', 'Start date' => '40658', 'Salary' => '320.8'],
53+
['Name' => 'Timothy Mooney', 'Position' => 'Office Manager', 'Office' => 'London', 'Age' => '37', 'Start date' => '39793', 'Salary' => '136.2'],
54+
['Name' => 'Unity Butler', 'Position' => 'Marketing Designer', 'Office' => 'San Francisco', 'Age' => '47', 'Start date' => '40156', 'Salary' => '85.675'],
55+
['Name' => 'Vivian Harrell', 'Position' => 'Financial Controller', 'Office' => 'San Francisco', 'Age' => '62', 'Start date' => '39858', 'Salary' => '452.5'],
56+
['Name' => 'Yuri Berry', 'Position' => 'Chief Marketing Officer (CMO)', 'Office' => 'New York', 'Age' => '40', 'Start date' => '39989', 'Salary' => '675'],
57+
['Name' => 'Zenaida Frank', 'Position' => 'Software Engineer', 'Office' => 'New York', 'Age' => '63', 'Start date' => '40182', 'Salary' => '125.25'],
58+
['Name' => 'Zorita Serrano', 'Position' => 'Software Engineer', 'Office' => 'San Francisco', 'Age' => '56', 'Start date' => '41061', 'Salary' => '115'],
59+
];

examples/libexample.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,73 @@ function var_dump2($value) {
55
var_export($value);
66
echo "</pre>";
77
}
8+
9+
/**
10+
* Generate a html table from an array
11+
*
12+
* @param array|null $array input array
13+
* @param string|bool $css if true then it uses the build in style. If false then it doesn't use style. If string then it uses as class
14+
*
15+
* @return string
16+
* @see https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array
17+
*/
18+
function generateTable($array, $css = true): string
19+
{
20+
if (!isset($array[0])) {
21+
$tmp = $array;
22+
$array = array();
23+
$array[0] = $tmp;
24+
} // create an array with a single element
25+
if ($array[0] === null) {
26+
return "NULL<br>";
27+
}
28+
if ($css === true) {
29+
$html = '<style>.generateTable {
30+
border-collapse: collapse;
31+
width: 100%;
32+
}
33+
.generateTable td, .generateTable th {
34+
border: 1px solid #ddd;
35+
padding: 8px;
36+
}
37+
.generateTable tr:nth-child(even){background-color: #f2f2f2;}
38+
.generateTable tr:hover {background-color: #ddd;}
39+
.generateTable th {
40+
padding-top: 12px;
41+
padding-bottom: 12px;
42+
text-align: left;
43+
background-color: #4CAF50;
44+
color: white;
45+
}
46+
</style>';
47+
} else {
48+
$html = '';
49+
}
50+
$html .= '<table class="' . (is_string($css) ? $css : 'generateTable') . '">';
51+
// header row
52+
$html .= '<thead><tr><th>#id</th>';
53+
foreach ($array[0] as $key => $value) {
54+
$html .= '<th>' . htmlspecialchars($key) . '</th>';
55+
}
56+
$html .= '</tr></thead>';
57+
58+
// data rows
59+
foreach ($array as $key=>$value) {
60+
$html .= '<tr>';
61+
$html .= '<td>' . htmlspecialchars($key) . '</td>';
62+
foreach ($value as $value2) {
63+
if (is_array($value2)) {
64+
$html .= '<td>' . generateTable($value2) . '</td>';
65+
} else {
66+
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
67+
}
68+
69+
}
70+
$html .= '</tr>';
71+
}
72+
73+
// finish table and return it
74+
75+
$html .= '</table>';
76+
return $html;
77+
}

0 commit comments

Comments
 (0)