PHP code example of dimsog / arrayhelper
1. Go to this page and download the library: Download dimsog/arrayhelper 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/ */
dimsog / arrayhelper example snippets
ArrayHelper::fluent($sourceArray)
->toInt(['id', 'parent_id'])
->except(['some_field'])
->filter(['user_id' => 100])
->get();
// or
Arr::fluent([[1], [2], [3]])
->collapse()
->map(function($item) {
return $item * 2;
})
->get();
ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]);
// or
Arr::collapse([[1, 2, 3], [4, 5, 6]]);
ArrayHelper::camelCaseKeys(array $source)
$data = ArrayHelper::camelCaseKeys([
'demo_field' => 100
]);
// result ($data):
[
'demoField' => 100
]
ArrayHelper::collapse(array $array)
$result = ArrayHelper::collapse([[1, 2, 3], [4, 5, 6]]);
result: [1, 2, 3, 4, 5, 6]
$result = ArrayHelper::collapse([1, 2, 3, [4], [5, 6]]);
result: [1, 2, 3, 4, 5, 6]
ArrayHelper::column(array $array, $key)
$array = [
[
'id' => 1,
'name' => 'test1'
],
[
'id' => 2,
'name' => 'test2'
]
];
ArrayHelper::column($array, 'id');
result: [1, 2]
ArrayHelper::chunk(array $array, $column = 2)
$array = [
'foo' => 'bar',
'bar' => 'baz',
'vodka' => 'balalayka'
];
$result = ArrayHelper::chunk($array, 2)
result: [
[
'foo' => 'bar',
'bar' => 'baz'
],
[
'vodka' => 'balalayka'
]
];
ArrayHelper::except(array $array, array $keys)
ArrayHelper::except(['a', 'b', 'c'], ['a', 'b']);
result: ['c']
ArrayHelper::exist(array $array, $condition)
ArrayHelper::exist(['a', 'b', 'c'], 'b'); // true
$array = [
[
'id' => 100,
'name' => 'Product 1'
],
[
'id' => 200,
'name' => 'Product 2'
],
[
'id' => 300,
'name' => 'Product 3'
]
];
ArrayHelper::exist($array, ['id' => 200]); // true
ArrayHelper::exist($array, function($item) {
return $item['id'] == 300;
});
ArrayHelper::filter(array $array, $condition, $preserveKeys = false)
$array = [
[
'id' => 1,
'category_id' => 5,
'name' => 'test1'
],
[
'id' => 3,
'category_id' => 1,
'name' => 'test3'
],
];
ArrayHelper::filter($array, ['category_id' => 5]);
// OR
ArrayHelper::filter($array, function($item) {
return $item['category_id'] == 5;
});
result:
[
[
'id' => 1,
'category_id' => 5,
'name' => 'test1'
]
]
ArrayHelper::findFirst(array $array, $condition)
$array = [
[
'id' => 1,
'foo' => 'bar'
],
[
'id' => 2,
'foo' => 'baz'
],
[
'id' => 3,
'foo' => 'baz'
]
];
// find a first element using callback:
ArrayHelper::findFirst($array, function($element) {
return $element['foo'] == 'baz';
});
result:
[
'id' => 2,
'foo' => 'baz'
]
// find a first element using array condition:
ArrayHelper::findFirst($array, ['foo' => 'baz']);
result:
[
'id' => 2,
'foo' => 'baz'
]
ArrayHelper::firstKey(array $array)
$array = [
'a' => 1,
'b' => 2,
'c' => 3
];
ArrayHelper::firstKey($array);
// result: 'a'
ArrayHelper::insert(&$array, $key, $value = null)
$array = [
'id' => 1,
'name' => 'Dmitry R'
];
ArrayHelper::insert($array, 'country', 'Russia');
result:
[
'id' => 1,
'name' => 'Dmitry R',
'country' => 'Russia'
]
$array = [
[
'id' => 1,
'name' => 'Dmitry R'
],
[
'id' => 1,
'name' => 'Dmitry R'
]
];
ArrayHelper::insert($array, 'foo', 'bar');
result:
[
[
'id' => 1,
'name' => 'Dmitry R',
'foo' => 'bar'
],
[
'id' => 1,
'name' => 'Dmitry R',
'foo' => 'bar'
]
]
ArrayHelper::getValue($array, $key, $defaultValue = null)
ArrayHelper::get($array, $key, $defaultValue = null)
ArrayHelper::getValue($user, 'id');
// with callback default value
ArrayHelper::getValue($user, 'name', function() {
return "Dmitry R";
});
// Retrivies the value of a sub-array
$user = [
'photo' => [
'big' => '/path/to/image.jpg'
]
]
ArrayHelper::getValue($user, 'photo.big');
// Retrivies the value of a stdClass
$user = json_decode($userJsonString);
ArrayHelper::getValue($user, 'photo.big');
ArrayHelper::has(array $array, $key)
$array = [
'foo' => [
'bar' => 10
]
];
ArrayHelper::has($array, 'foo.bar')
// true
$array = [
'foo' => [
'bar' => [0, 1, 2, 'a']
]
];
ArrayHelper::has($array, 'foo.bar.1')
// true
ArrayHelper::ids($array)
$array = [
[
'id' => 1,
'name' => 'test1'
],
[
'id' => 2,
'name' => 'test2'
]
];
ArrayHelper::ids($array);
result: [1, 2]
ArrayHelper::isAssoc(array $array)
ArrayHelper::isAssoc([1, 2, 3]);
result: false
ArrayHelper::isAssoc(['foo' => 'bar']);
result: true
ArrayHelper::isMulti(array $array, $strongCheck = false)
$array = [
['foo' => 'bar'],
['foo' => 'bar']
];
ArrayHelper::isMulti($array);
ArrayHelper::keyValue(array $items, $keyField = 'key', $valueField = 'value')
$array = [
[
'key' => 'name',
'value' => 'Dmitry'
],
[
'key' => 'country',
'value' => 'Russia'
],
[
'key' => 'city',
'value' => 'Oryol (eagle)'
]
];
$array = ArrayHelper::keyValue($array, 'key', 'value');
result:
[
'name' => 'Dmitry',
'country' => 'Russia',
'city' => 'Oryol (eagle)'
];
ArrayHelper::lastKey($array)
$array = [
'a' => 1,
'b' => 2,
'c' => 3
];
ArrayHelper::lastKey($array);
// result: 'c'
ArrayHelper::map($array, \Closure $callback)
ArrayHelper::map($array, function($item) {
return $item;
});
ArrayHelper::only(array $array, array $keys)
ArrayHelper::only(['a', 'b', 'c'], ['a', 'b']);
result: ['a', 'b'];
$array = [
'foo' => 'bar',
'foo2' => 'bar2',
'foo3' => 'bar3'
];
ArrayHelper::only($array, ['foo2']);
result:
[
'foo2' => 'bar2'
]
$array = [
[
'foo' => 'bar',
'foo2' => 'bar2'
],
[
'foo' => 'bar',
'foo2' => 'bar2'
]
];
ArrayHelper::only($array, ['foo']);
result:
[
['foo' => 'bar'],
['foo' => 'bar']
]
$array = [
'foo' => 'bar',
'foo2' => 'bar2'
];
ArrayHelper::except($array, ['foo2']);
result: ['foo' => 'bar']
$array = [
[
'foo' => 'bar',
'foo2' => 'bar2'
],
[
'foo' => 'bar',
'foo2' => 'bar2'
]
];
ArrayHelper::except($array, ['foo2']);
result:
[
['foo' => 'bar'],
['foo' => 'bar']
]
ArrayHelper::onlyWithKey(array $array, $key)
$array = [
[
'a' => 1,
'b' => 2
],
[
'a' => 1,
'b' => 2
],
[
'b' => 2
]
];
ArrayHelper::onlyWithKey($array, 'a');
// result:
[
[
'a' => 1,
'b' => 2
],
[
'a' => 1,
'b' => 2
]
]
ArrayHelper::paginate(array $array, $page, $limit)
$array = [1, 2, 3, 4, 5, 6];
ArrayHelper::paginate($array, 1, 3)
result: [1, 2, 3]
ArrayHelper::prepend(&$array, $keyOrValue, $value = null)
$array = [
1, 2, 3, 4
];
ArrayHelper::prepend($array, -1);
result:
[-1, 1, 2, 3, 4]
$array = [
'foo' => 'bar'
];
ArrayHelper::prepend($array, 'test', 123);
result:
[
'test' => 123,
'foo' => 'bar'
];
ArrayHelper::random(array $array, $count = 1)
ArrayHelper::random([1, 2, 3])
result: 1 or 2 or 3
ArrayHelper::random([1, 2, 3], 2);
result: [1, 3]
ArrayHelper::reindex($array)
$array = [
1 => 10,
2 => 20
];
ArrayHelper::reindex($array);
result: [10, 20]
ArrayHelper::remove(array &$array, $keys)
$array = [
'foo' => [
'bar' => 'baz'
],
'foo1' => 123
];
ArrayHelper::remove($array, 'foo.bar');
// result:
[
'foo' => [],
'foo1' => 123
]
$array = [
[
'foo' => 'bar',
'test' => 'test1'
],
[
'foo' => 'bar',
'test' => 'test2'
]
];
ArrayHelper::remove($array, 'foo');
// result:
[
['test' => 'test1'],
['test' => 'test2']
]
$array = [
[
'foo' => [
'bar' => [
'baz' => 1
]
],
'test' => 'test',
'test2' => '123',
'only' => true
],
[
'foo' => [
'bar' => [
'baz' => 2
]
],
'test' => 'test',
'test2' => 123
]
];
ArrayHelper::remove($array, ['foo.bar.baz', 'test', 'only']);
// result:
[
[
'foo' => [
'bar' => []
],
'test2' => '123'
],
[
'foo' => [
'bar' => []
],
'test2' => 123
]
]
ArrayHelper::replaceKey($oldKey, $newKey, &$array)
$array = [
'foo' => 'bar'
];
ArrayHelper::replaceKey('foo', 'baz', $array);
// result ($array):
[
'baz' => 'bar'
]
ArrayHelper::set(array &$array, $key, $value)
$array = [
'product' => [
'name' => 'Some name',
'price' => 500
]
];
ArrayHelper::set($array, 'product.price', 600);
ArrayHelper::shuffle(array $array)
ArrayHelper::shuffle([1, 2, 3]);
result: [3, 1, 2]
ArrayHelper::sortBy(array $array, $key = null, $direction = 'ASC');
ArrayHelper::sortByAsc(array $array, $key = null);
ArrayHelper::sortByDesc(array $array, $key = null);
$array = [
['id' => 1],
['id' => 10]
];
$result = ArrayHelper::sortByDesc($array, 'id');
result:
[
['id' => 10],
['id' => 1]
];
ArrayHelper::splitString($str)
$string = 'Ab Cd';
ArrayHelper::splitString($string);
result: ['A', 'b', ' ', 'C', 'd']
ArrayHelper::sum(array $array, $key)
$array = [
[
'name' => 'entity1',
'total' => 5
],
[
'name' => 'entity2',
'total' => 6
]
];
$result = ArrayHelper::sum($array, 'total');
// result: 11
ArrayHelper::toArray([stdClassInstance, stdClassInstance, someMixedClass]);
ArrayHelper::toArray('{"foo":{"bar":123}}');
class SimpleClass
{
public $test = null;
public function __construct()
{
$std = new \stdClass();
$std->f = (new \stdClass());
$std->f->b = [1, 2];
$this->test = [$std, ['a', 'b']];
}
}
class SimpleIteratorTestClass implements \Iterator
{
// ...
private $array = [];
public function __construct()
{
$this->position = 0;
$std = new \stdClass();
$std->f = (new \stdClass());
$std->f->b = [1, 2];
$this->array = [1, 2, 3, 4, 'asd', $std, new SimpleClass(), ['yeah' => [1, 2]]];
}
// ...
}
ArrayHelper::toArray(new SimpleIteratorTestClass());
result:
[
1, 2, 3, 4, 'asd',
[
'f' => [
'b' => [1, 2]
]
],
[
'test' => [
[
'f' => [
'b' => [1, 2]
]
],
[
'a', 'b'
]
]
],
[
'yeah' => [1, 2]
]
]
ArrayHelper::toInt(array $source, array $keys = [])
$source = [
"id" => "100",
"created_at" => "10000",
"name" => "Foo Bar"
];
$source = ArrayHelper::toInt($source, ["id" "created_at"]);
// result:
[
"id" => 100,
"created_at" => 10000,
"name" => "Foo Bar"
]
// Convert all values:
$source = ArrayHelper::toInt($source);
// Since 1.1.0
$source = [
[
'id' => '100',
'created_at' => '1000',
'name' => 'FooBar',
'other' => '200'
],
[
'id' => '200',
'created_at' => '2000',
'name' => 'FooBar',
'other' => '300'
]
];
ArrayHelper::toInt($source, ['id', 'created_at', 'other']);
ArrayHelper::unique(array $array, $key = null)
$array = [
'a', 'a', 'b', 'b'
];
$array = ArrayHelper::unique($array);
// result:
['a', 'b']
$array = [
['a', 'a', 'b', 'b'],
['a', 'a', 'b', 'b']
];
$array = ArrayHelper::unique($array);
// result:
[
['a', 'b'],
['a', 'b']
]
$array = [
[
'id' => 100,
'name' => 'Product 1'
],
[
'id' => 200,
'name' => 'Product 2'
],
[
'id' => 100,
'name' => 'Product 3'
],
[
'name' => 'Product 4'
]
];
$array = ArrayHelper::unique($array, 'id');
// result:
[
[
'id' => 100,
'name' => 'Product 1'
],
[
'id' => 200,
'name' => 'Product 2'
]
]
ArrayHelper::values(array $array)
$array = [
'name' => 'Dmitry R',
'country' => 'Russia',
'skills' => ['PHP', 'JS'],
[
'identifier' => 'vodka medved balalayka'
]
];
ArrayHelper::values($array);
// result:
['Dmitry R', 'Russia', 'PHP', 'JS', 'vodka medved balalayka'];
ArrayHelper::wrap($value)
ArrayHelper::wrap('123');
// result: ['123']