Skip to content

Extend_Arrays

StefansArya edited this page Apr 18, 2019 · 1 revision

This feature will help you when doing string any manipulation.
You may need to import this feature on top of your code.

use \Scarlets\Extend\Arrays as Arr;

duplicates

Obtain duplicated value in the array.

Arr::duplicates($array);

# Example
$data = Arr::duplicates(['henlo', 'hello', 'henlos', 'hello', 'hellow']);

// Output: ['hello']

uniqueDifference

Find difference between 2 array and return new value, unchanged value, and deleted value from the first array.

Arr::uniqueDifference($array1, $array2);

# Example
echo Arr::uniqueDifference([1,3,2,4], [3,5,4,6]);

/* Output: [
    'new'=>[5,6],
    'unchanged'=>[3,4],
    'deleted'=>[1,2],
]*/

encodeComma

This is supposed to work with Database. Turn array into comma separate number.

Arr::encodeComma(&$arr);

# Example
$arr = [1,2,3];
echo Arr::encodeComma($arr);

// Output: ,1,2,3,

decodeComma

This is supposed to work with Database. Turn comma separated number into array.

Arr::decodeComma(&$arr);

# Example
$str = ',1,2,3,';
echo Arr::decodeComma($str);

// Output: [1,2,3]

scoreSimillar

Calculate similarity score for a value in the array of value.

Arr::scoreSimillar(&$cache, $value, $column = null, $id = null);

# Example
$data = ['henlo', 'hello', 'henlos', 'hellow', 'helio'];

$return = Arr::scoreSimillar($data, 'henlow');
/*
    [0] => 90.909090909091 #-> henlo
    [2] => 83.333333333333 #-> henlos
    [3] => 83.333333333333 #-> hellow
    [1] => 72.727272727273 #-> hello
    [4] => 72.727272727273 #-> helio
 */

You can also use associative array for scoring the similarity of a column value and assign the key from the related row.

$data = [
    ['code'=>'A', 'value'=>'henlo'],
    ['code'=>'B', 'value'=>'hello'],
    ['code'=>'C', 'value'=>'henlos'],
    ['code'=>'D', 'value'=>'hellow'],
    ['code'=>'E', 'value'=>'helio']
];

$return = Arr::scoreSimillar($data, 'henlow', 'value', 'code');
/*
    [A] => 90.909090909091 #-> henlo
    [C] => 83.333333333333 #-> henlos
    [D] => 83.333333333333 #-> hellow
    [B] => 72.727272727273 #-> hello
    [E] => 72.727272727273 #-> helio
 */
Clone this wiki locally