PHP Classes

PHP Matrix Library: Perform matrix manipulation operations with arrays

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-12 (6 months ago) RSS 2.0 feedNot enough user ratingsTotal: 33 All time: 11,057 This week: 73Up
Version License PHP version Categories
matriztools 1.0.0MIT/X Consortium ...7.0Tools, Data types, PHP 7
Description 

Author

This package is specific mainly for applications used in Brazil Brazil .

This class can perform matrix manipulation operations with arrays.

It provides functions that can perform several types of operations on matrixes that are stored internally as arrays.

Currently, it can:

- Search the matrix using a given key or a given value

- Count the number of values in the matrix

- Search values in the matrix and replace them with new values

- Sum all values in the matrix

- Remove elements with a given value

- Sort the matrix by value and by key

- Merge two matrixes

- Check if matrix is multi-dimensional


This class provides methods for performing operations on multidimensional arrays, such as searching for a specific key.

Picture of Leonardo Feitosa
  Performance   Level  
Name: Leonardo Feitosa <contact>
Classes: 3 packages by
Country: Brazil Brazil
Age: 37
All time rank: 4480363 in Brazil Brazil
Week rank: 180 Up12 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Instructions

Examples of Using the MatrixTools Class

The MatrixTools class provides various functions for performing operations on multidimensional arrays. Below are examples of how to use each of them.

1. searchByKey

$matrix = [
    'name' => 'John',
    'age' => 30,
    'address' => [
        'city' => 'New York',
        'zip' => '10001',
    ],
];
$result = MatrixTools::searchByKey('city', $matrix);
print_r($result);

2. searchByValue

$matrix = [
    'apple',
    'banana',
    ['orange', 'kiwi'],
    'apple',
    ['banana', 'grape'],
];

$result = MatrixTools::searchByValue('banana', $matrix);
print_r($result);

3. countOccurrences

$matrix = [
    'apple',
    'banana',
    ['orange', 'kiwi'],
    'apple',
    ['banana', 'grape'],
];

$result = MatrixTools::countOccurrences($matrix);
print_r($result);

4. replaceValue

$matrix = [
    'apple',
    'banana',
    ['orange', 'kiwi'],
    'apple',
    ['banana', 'grape'],
];

$result = MatrixTools::replaceValue('banana', 'pear', $matrix);
print_r($result);

5. replaceKey

$matrix = [
    'name' => 'John',
    'age' => 30,
    'address' => [
        'city' => 'New York',
        'zip' => '10001',
    ],
];

$result = MatrixTools::replaceKey('city', 'town', $matrix);
print_r($result);

6. sumValues

$matrix = [
    1,
    [2, 3],
    [4, [5, 6]],
];

$result = MatrixTools::sumValues($matrix);
echo $result;

7. removeElementByValue

$matrix = [
    'apple',
    'banana',
    ['orange', 'kiwi'],
    'apple',
    ['banana', 'grape'],
];

$result = MatrixTools::removeElementByValue('banana', $matrix);
print_r($result);

8. removeElementByKey

$matrix = [
    'name' => 'John',
    'age' => 30,
    'address' => [
        'city' => 'New York',
        'zip' => '10001',
    ],
];

$result = MatrixTools::removeElementByKey('city', $matrix);
print_r($result);

9. sortByValue

$matrix = [
    'apple',
    'banana',
    'grape',
    'orange',
];

$result = MatrixTools::sortByValue($matrix, 'desc');
print_r($result);

10. sortByKey

$matrix = [
    'name' => 'John',
    'age' => 30,
    'address' => [
        'city' => 'New York',
        'zip' => '10001',
    ],
];

$result = MatrixTools::sortByKey($matrix, 'desc');
print_r($result);

11. mergeMatrices

$matrix1 = [
    'name' => 'John',
    'age' => 30,
    'address' => [
        'city' => 'New York',
        'zip' => '10001',
    ],
];

$matrix2 = [
    'address' => [
        'state' => 'NY',
    ],
    'phone' => '123-456-7890',
];

$result = MatrixTools::mergeMatrices($matrix1, $matrix2);
print_r($result);

12. isMultidimensional

$matrix = ['apple', 'banana', 'orange'];

$result = MatrixTools::isMultidimensional($matrix);
var_dump($result);
  Files folder image Files (2)  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Plain text file Matrix.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:33
This week:0
All time:11,057
This week:73Up