PHP Classes

PHP Matrix Library: Perform matrix manipulation operations with arrays

Recommend this page to a friend!
  Info   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-03-12 (25 days ago) RSS 2.0 feedNot enough user ratingsTotal: 30 This week: 2All time: 11,038 This week: 49Up
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: 1 package by
Country: Brazil Brazil

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  
File Role Description
Accessible without login Plain text file LICENSE Lic. License text
Plain text file Matrix.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:30
This week:2
All time:11,038
This week:49Up