jmasci/matrix-builder

此包的最新版本(dev-master)没有提供许可证信息。

一个可变矩阵对象,具有排序行/列、添加/删除单元格以及其他一些方法。

dev-master 2022-01-23 19:57 UTC

This package is auto-updated.

Last update: 2024-09-24 01:48:38 UTC


README

在这个上下文中,矩阵是一个索引数组的索引数组。这个库是一个类,允许您逐步构建这样的矩阵,并提供一些有用的行和列方法,例如简单的获取/设置器、删除整个行或列以及排序行或列。

将这个操作封装成类/封装是否是最好的方法?不确定。

我们难道不能只使用原始数据结构并编写纯函数吗?可能吧。我之所以使用类,是因为列操作必须与行操作不同,并且通常更复杂。因此,对于每个操作,似乎更方便只编写一个行方法和一个列方法。

我本可以编写一个函数来将行转换为列(以及将列转换为行)。然后,任何用于操作行的函数都可以通过在行操作前后调用“逆函数”来实现对列的操作(希望这说得通)。无论如何,对于这个库,所有功能都封装在一个类中,这个类主要封装了其数据,但如果需要,不会阻止您操作内部数据结构。

基本用法

一般来说,每个行方法都有一个相应的列方法。

use JMasci\MatrixBuilder;

// create a new empty matrix
$matrix = new MatrixBuilder();

// sets some values.
$matrix->set( 'row_1', 'col_1', 'value 1,1' );
$matrix->set( 'row_1', 'col_2', 'value 1,2' );
$matrix->set( 'row_2', 'col_1', 'value 2,1' );
$matrix->set( 'row_2', 'col_2', 'value 2,2' );

// returns "value 1,2"
$matrix->get( 'row_1', 'col_2' );

// returns [ 'row_1' => 'value 1,1', 'row_2' => 'value 2,1' ]
$matrix->get_column( 'col_1' );

// returns [ 'col_1' => 'value 1,1', 'col_2' => 'value 1,2' ]
$matrix->get_row( 'row_1' );

print_r( $matrix->get_matrix() );
gives you:

Array
(
    [row_1] => Array
        (
            [col_1] => value 1,1
            [col_2] => value 1,2
        )

    [row_2] => Array
        (
            [col_1] => value 2,1
            [col_2] => value 2,2
        )

)

排序、删除和设置总计

// puts the given rows first. If you pass in a row that doesn't exist it will ignore it.
$matrix->apply_row_sort( [ 'row_2', 'row_1' ]);

// alternate sort method accepting an anonymous function. This would sort columns alphabetically.
$matrix->sort_rows( function( $keys ) {
    asort( $keys );
    return $keys;
});

// the set function also accepts an anonymous function, which will be provided the previous value.
// the value afterwards will be 100.
$matrix->set( 'row_1', 'col_1', 95 ); 
$matrix->set( 'row_1', 'col_1', $matrix::get_incrementer( 5 ) );

$matrix->delete_row( 'row_2' );

// adds a new column to each row whose value is determined by the callback function.
// useful when your values are numeric.
$matrix->set_row_totals( function( $row, $key ) { return array_sum( $row ); }, 'total' );

实际案例

您可以在SQL中使用group by和count做类似的事情。但在PHP中构建矩阵时,灵活性更高。

use JMasci\MatrixBuilder;

$matrix = new MatrixBuilder();

foreach ( query_posts_and_join_authors() as $post ) {
    // each time we call set we either add 1 to the previous value 
    // or initialize a new row and/or column and then add 1.
    $matrix->set( $post->author_name, date( 'm Y', strtotime( $post->post_date ) ), $matrix::get_incrementer(1));
}

// assume the query already sorted by date. Rows will remain sorted in the order that they were added. 

// sort authors by name
$matrix->sort_columns( function( $keys ){
    sort( $keys );
    return $keys;
});

$matrix->set_row_totals( $matrix::get_array_summer(), '__Total' );
$matrix->set_column_totals( $matrix::get_array_summer(), '__Total' );

$data = $matrix->convert_to_record_set_with_headings( "Authors vs. Post Dates");

用于渲染表格等...

可能的未来功能:允许单元格使用公式。但这可能会变得复杂。