PHP code example of marsapp / maphelper

1. Go to this page and download the library: Download marsapp/maphelper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marsapp / maphelper example snippets




// Use namespace
use marsapp\helper\mapping\MapHelper;

// Data
$data = [
    ['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'],
    ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2'],
];

// Index by
MapHelper::add($data, 'userInfo', ['c_sn', 'u_no']);

// Get name by a110 => a001 => u_name
$name = MapHelper::getMap('userInfo', ['a110', 'a001', 'u_name']);
// $name = name1;

init(string $mapName = null) : MapHelper

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Clear the Map cache with the specified name, like: userInfo.
MapHelper::init('userInfo');

//Clear all Map cache.
MapHelper::init();

add(array $data, string $mapName, array $indexBy, string $indexType = 'indexBy') : MapHelper

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Data
$data = [['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2']];

// Store to map cache - Useing indexBy
MapHelper::add($data, 'map1', ['c_sn', 'u_sn'], 'indexBy');
// Get Map data
$result = MapHelper::getMap('map1');
var_export($result);
// Output: 
// array (
//   'a110' => 
//   array (
//     'b1' => 
//     array (
//       'c_sn' => 'a110',
//       'u_sn' => 'b1',
//       'u_no' => 'a001',
//       'u_name' => 'name1',
//     ),
//     'b2' => 
//     array (
//       'c_sn' => 'a110',
//       'u_sn' => 'b2',
//       'u_no' => 'b012',
//       'u_name' => 'name2',
//     ),
//   ),
// )


// Store to map cache - Useing groupBy
MapHelper::add($data, 'map2', ['c_sn', 'u_sn'], 'groupBy');
// Get Map data
$result = MapHelper::getMap('map2');
var_export($result);
// Output: 
// array (
//   'a110' => 
//   array (
//     'b1' => 
//     array (
//       0 => 
//       array (
//         'c_sn' => 'a110',
//         'u_sn' => 'b1',
//         'u_no' => 'a001',
//         'u_name' => 'name1',
//       ),
//     ),
//     'b2' => 
//     array (
//       0 => 
//       array (
//         'c_sn' => 'a110',
//         'u_sn' => 'b2',
//         'u_no' => 'b012',
//         'u_name' => 'name2',
//       ),
//     ),
//   ),
// )


// Store to map cache - Useing indexOnly
MapHelper::add($data, 'map3', ['c_sn', 'u_sn'], 'indexOnly');
// Get Map data
$result = MapHelper::getMap('map3');
var_export($result);
// Output: 
// array (
//   'a110' => 
//   array (
//     'b1' => '',
//     'b2' => '',
//   ),
// )

addRaw(array $data, string $mapName) : MapHelper

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Data
$data = [['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2']];

// Store to map cache
MapHelper::addRaw($data, 'map1');
// Get Map data
$result = MapHelper::getMap('map1');
var_export($result);
// Output: 
// [['c_sn' => 'a110', 'u_sn' => 'b1', 'u_no' => 'a001', 'u_name' => 'name1'], ['c_sn' => 'a110', 'u_sn' => 'b2', 'u_no' => 'b012', 'u_name' => 'name2']]

sort(string $mapName, $type = 'ksort') : MapHelper

// Use namespace
use marsapp\helper\mapping\MapHelper;

$data = ['1111' => 'name1', '3333' => 'name3', '2222' => 'name2'];

// Add map
MapHelper::addRaw($data, 'testMap');


// Sort Map and output
$result = MapHelper::sort('testMap', 'ksort')->getMap('testMap');
var_export($result);
// Output: ['1111' => 'name1', '2222' => 'name2', '3333' => 'name3']


// Sort Map and output
$result = MapHelper::sort('testMap', 'krsort')->getMap('testMap');
var_export($result);
// Output: ['3333' => 'name3', '2222' => 'name2', '1111' => 'name1']


getMap(string $mapName, array $indexBy = [], $exception = false) : mixed

// Use namespace
use marsapp\helper\mapping\MapHelper;

$data = ['1111' => 'name1', '3333' => 'name3', '2222' => 'name2'];

// Add map
MapHelper::addRaw($data, 'testMap');
$result = MapHelper::getMap('testMap');
var_export($result);
// Output: ['1111' => 'name1', '3333' => 'name3', '2222' => 'name2']

location(string $mapName, array $indexBy, string $location) : mixed

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Data
$data = ['lv1' => ['lv2' => ['2011-05-06' => '11', '2012-01-01' => '12', '2015-09-16' => '15']]];

// Add map
MapHelper::addRaw($data, 'testMap');

// Get data by location point.
$result = MapHelper::location('testMap', ['lv1', 'lv2'], '2011-12-05');
var_export($result);
// Output: 11

// Get data by location point.
$result = MapHelper::location('testMap', ['lv1', 'lv2'], '2012-12-05');
var_export($result);
// Output: 12

hasMap(string $mapName) : boolean

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Add map
MapHelper::addRaw([1, 2, 3, 4], 'map1');

// Determine whether the target map table exists
$mapName = 'map1';
if (MapHelper::hasMap($mapName)) {
    echo $mapName . ' Exist.';
} else {
    echo $mapName . ' Not exist.';
}

mapList() : array

// Use namespace
use marsapp\helper\mapping\MapHelper;

// Add map
MapHelper::addRaw([1, 2, 3, 4], 'map1');
MapHelper::addRaw(['a', 'b'], 'map2');

// Get map name list and print it.
$mapList = MapHelper::mapList();
var_export($mapList);
// output: ['map1', 'map2']