PHP code example of me-io / php-lodash
1. Go to this page and download the library: Download me-io/php-lodash 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/ */
me-io / php-lodash example snippets
__::append([1, 2, 3], 4);
# [1, 2, 3, 4]
__::compact([0, 1, false, 2, '', 3]);
# [1, 2, 3]
__::flatten([1, 2, [3, [4]]], [flatten]);
# [1, 2, 3, 4]
__::patch(['addr' => ['country' => 'US', 'zip' => 12345]], ['/addr/country' => 'CA', '/addr/zip' => 54321]);
# ['addr' => ['country' => 'CA', 'zip' => 54321]]
__::prepend([1, 2, 3], 4);
# [4, 1, 2, 3]
__::range(1, 10, 2);
# [1, 3, 5, 7, 9]
__::repeat('foo', 3);
# ['foo', 'foo', 'foo']
__::chunk([1, 2, 3, 4, 5], 3);
# [[1, 2, 3], [4, 5]]
__::drop([0, 1, 3], 2);
# [3]
__::randomize([1, 2, 3]);
# [2, 3, 1]
__::search(['a', 'b', 'c'], 'b');
# 1
__::average([1, 2, 3]);
# 2
__::size([1, 2, 3]);
# 3
__::contains(['a', 'b', 'c'], 'b');
# true
__::clean([true, false, 0, 1, 'string', '']);
# [true, 1, 'string']
__::random([1, 2, 3]);
# Returns 1, 2 or 3
__::intersection(["green", "red", "blue"], ["green", "yellow", "red"]);
# ["green", "red"]
__::intersects(["green", "red", "blue"], ["green", "yellow", "red"])
# true
__::initial([1, 2, 3], 1);
# [1, 2]
__::rest([1, 2, 3], 2);
# [3]
__::sortKeys(['z' => 0, 'b' => 1, 'r' => 2]);
# ['b' => 1, 'r' => 2, 'z' => 0]
__::sortKeys(['z' => 0, 'b' => 1, 'r' => 2], 'desc');
# ['z' => 0, 'r' => 2, 'b' => 1]
__::without([1, 1 => 3, 2 => 4, 5], 4)
# [0 => 1, 1 => 3, 2 => 5]
__::without([1, 3 => 3, 2 => 4, 5], 4, true)
# [0 => 1, 3 => 3, 4 => 5]
__::chain([0, 1, 2, 3, null])
->compact()
->prepend(4)
->value();
# [4, 1, 2, 3]
$a = [
['name' => 'fred', 'age' => 32],
['name' => 'maciej', 'age' => 16]
];
__::filter($a, function($n) {
return $n['age'] > 24;
});
# [['name' => 'fred', 'age' => 32]]
__::first([1, 2, 3, 4, 5], 2);
# [1, 2]
__::get(['foo' => ['bar' => 'ter']], 'foo.bar');
# 'ter'
__::last([1, 2, 3, 4, 5], 2);
# [4, 5]
__::map([1, 2, 3], function($n) {
return $n * 3;
});
# [3, 6, 9]
__::max([1, 2, 3]);
# 3
__::min([1, 2, 3]);
# 1
$a = [
['foo' => 'bar', 'bis' => 'ter' ],
['foo' => 'bar2', 'bis' => 'ter2'],
];
__::pluck($a, 'foo');
# ['bar', 'bar2']
$a = [
['name' => 'fred', 'age' => 32],
['name' => 'maciej', 'age' => 16]
];
__::where($a, ['age' => 16]);
# [['name' => 'maciej', 'age' => 16]]
$a = [
'color' => [
'favorite' => 'red',
5
],
3
];
$b = [
10,
'color' => [
'favorite' => 'green',
'blue'
]
];
__::assign($a, $b);
# ['color' => ['favorite' => 'green', 'blue'], 10]
__::reduceRight(['a', 'b', 'c'], function ($word, $char) {
return $word . $char;
}, '');
# 'cba'
__::doForEachRight([1, 2, 3], function ($value) { print_r($value) });
# (Side effect: print 3, 2, 1)
__::doForEach([1, 2, 3], function ($value) { print_r($value) });
# (Side effect: print 1, 2, 3)
__::set(['foo' => ['bar' => 'ter']], 'foo.baz.ber', 'fer');
# '['foo' => ['bar' => 'ter', 'baz' => ['ber' => 'fer']]]'
__::hasKeys(['foo' => 'bar', 'foz' => 'baz'], ['foo', 'foz']);
# true
__::has(['foo' => ['bar' => 'num'], 'foz' => 'baz'], 'foo.bar');
# true
__::hasKeys((object) ['foo' => 'bar', 'foz' => 'baz'], 'bar');
# false
__::concat(['color' => ['favorite' => 'red', 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]);
# ['color' => ['favorite' => ['green'], 5, 'blue'], 3, 10]
__::concatDeep(['color' => ['favorite' => 'red', 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]);
# ['color' => ['favorite' => ['red', 'green'], 5, 'blue'], 3, 10]
__::ease(['foo' => ['bar' => 'ter'], 'baz' => ['b', 'z']]);
# '['foo.bar' => 'ter', 'baz.0' => 'b', , 'baz.1' => 'z']'
__::every([1, 3, 4], function ($v) { return is_int($v); });
// → true
__::groupBy([
['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'],
],
'state'
);
# [
# 'IN' => [
# ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
# ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
# ],
# 'CA' => [
# ['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen']
# ]
# ]
__::groupBy([
['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
],
function ($value) {
return $value->city;
}
);
# [
# 'Indianapolis' => [
# ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
# ['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
# ],
# 'San Diego' => [
# ['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
# ]
# ]
__::isEmpty([]);
# true
__::merge(['color' => ['favorite' => 'red', 'model' => 3, 5], 3], [10, 'color' => ['favorite' => 'green', 'blue']]);
# ['color' => ['favorite' => 'green', 'model' => 3, 'blue'], 10]
__::pick(['a' => 1, 'b' => ['c' => 3, 'd' => 4]], ['a', 'b.d']);
# ['a' => 1, 'b' => ['d' => 4]]
__::reduce([1, 2], function ($sum, $number) {
return $sum + $number;
}, 0);
# 3
$a = [
['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'School bus'],
['state' => 'IN', 'city' => 'Indianapolis', 'object' => 'Manhole'],
['state' => 'IN', 'city' => 'Plainfield', 'object' => 'Basketball'],
['state' => 'CA', 'city' => 'San Diego', 'object' => 'Light bulb'],
['state' => 'CA', 'city' => 'Mountain View', 'object' => 'Space pen'],
];
$iteratee = function ($accumulator, $value) {
if (isset($accumulator[$value['city']]))
$accumulator[$value['city']]++;
else
$accumulator[$value['city']] = 1;
return $accumulator;
};
__::reduce($c, $iteratee, []);
# [
# 'Indianapolis' => 2,
# 'Plainfield' => 1,
# 'San Diego' => 1,
# 'Mountain View' => 1,
# ]
$object = new \stdClass();
$object->a = 1;
$object->b = 2;
$object->c = 1;
__::reduce($object, function ($result, $value, $key) {
if (!isset($result[$value]))
$result[$value] = [];
$result[$value][] = $key;
return $result;
}, [])
# [
# '1' => ['a', 'c'],
# '2' => ['b']
# ]
__::unease(['foo.bar' => 'ter', 'baz.0' => 'b', , 'baz.1' => 'z']);
# '['foo' => ['bar' => 'ter'], 'baz' => ['b', 'z']]'
__::split('a-b-c', '-', 2);
# ['a', 'b-c']
__::camelCase('Foo Bar');
# 'fooBar'
__::capitalize('FRED');
# 'Fred'
__::kebabCase('Foo Bar');
# 'foo-bar'
__::lowerFirst('Fred');
# 'fred'
__::snakeCase('Foo Bar');
# 'foo_bar'
__::startCase('--foo-bar--');
# 'Foo Bar'
__::toLower('fooBar');
# 'foobar'
__::toUpper('fooBar');
# 'FOOBAR'
__::upperCase('--foo-bar');
# 'FOO BAR'
__::upperFirst('fred');
# 'Fred'
__::words('fred, barney, & pebbles');
# ['fred', 'barney', 'pebbles']
__::words('fred, barney, & pebbles', '/[^, ]+/');
# ['fred', 'barney', '&', 'pebbles']
__::lowerCase('--Foo-Bar--');
# 'foo bar'
__::slug('Jakieś zdanie z dużą ilością obcych znaków!');
# 'jakies-zdanie-z-duza-iloscia-obcych-znakow'
$options = [
'delimiter' => '-',
'limit' => 30,
'lowercase' => true,
'replacements' => array(),
'transliterate' => true
]
__::slug('Something you don\'t know about know about Jackson', $options);
# 'something-you-dont-know-about'
$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque et mi orci.';
__::truncate($string);
# 'Lorem ipsum dolor sit amet, consectetur...'
__::truncate($string, 60);
# 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pel...'
__::urlify('I love https://google.com');
# 'I love <a href="https://google.com">google.com</a>'
__::isArray([1, 2, 3]);
# true
__::isArray(123);
# false
__::isFunction(function ($a) { return $a + 2; });
# true
__::isNull(null);
# true
__::isNumber(123);
# true
__::isObject('fred');
# false
__::isString('fred');
# true
__::isEmail('[email protected] ');
# true
__::isEmail('test_test.com');
# false
__::now();
# 1417546029
__::stringContains('waffle', 'wafflecone');
# true
ini
extension=php_mbstring.dll