1. Go to this page and download the library: Download andydune/array-container 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/ */
andydune / array-container example snippets
use AndyDune\ArrayContainer\ArrayContainer;
$aray = new ArrayContainer(['pipe' => 'handemade', 'pipes_type' => ['lovat', 'canadian']]);
$array['pipe'] // value is 'handemade'
$array['tobacco'] // value is null
$array->getNested('pipes_type.0') // value is 'lovat'
$array->getNested('pipes_type:0', null, ':') // value is 'lovat'
$array->getNested('some.some', 'NO') // value is 'NO'
// Set default value
$array->setDefaultValue('NO');
$array['tobacco'] // value is 'NO'
use AndyDune\ArrayContainer\ArrayContainer;
$aray = new ArrayContainer(['pipe' => 'handemade', 'pipes_type' => ['lovat', 'canadian']]);
$array->addFilter(function ($value) {
return strtoupper($value);
});
$array['pipe'] // value is 'HANDEMADE'
$array = [
'type' => 'good'
];
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\KeysAddIfNoExist;
$container = new ArrayContainer();
$defaultValue = 0; // Set this value if array key does not exist.
$container->setAction(new KeysAddIfNoExist($defaultValue))->executeAction('type', 'key', 'value');
$resultArray = $container->getArrayCopy();
use AndyDune\ArrayContainer\Action\SetValueIntoNestedArray;
use AndyDune\ArrayContainer\ArrayContainer;
$arrayContainer = new ArrayContainer();
foreach($data as $row) {
$arrayContainer->setAction(new SetValueIntoNestedArray($row['orderCount']))
->executeAction($row['year'], $row['month']);
}
$result = $arrayContainer->getArrayCopy();
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\RemoveDuplicates;
$array = [
'a' => 'a',
'b' => 'b',
'b1' => 'b',
'c' => 'c',
];
$container = new ArrayContainer($array);
$count = $container->setAction(new RemoveDuplicates())->executeAction();
$count == 1; // it is count of removed items
$array = $container->getArrayCopy());
$array; // it has no value with key b1
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\InNestedArray;
$array = [
'a' => 1,
'b' => [
'c' => 2
]
];
$container = new ArrayContainer($array);
$container->setAction(new InNestedArray(1))->executeAction(); // true
$container->setAction(new InNestedArray('1'))->executeAction(); // true
$container->setAction(new InNestedArray(5))->executeAction(); // false
// With strong type comparision
$container->setAction(new InNestedArray('1', true))->executeAction(); // false
$array = [
[
[
'name' => 'Ivan'
],
[
'name' => 'Andrey'
],
]
];
$container = new ArrayContainer($array);
// false: Ivan != ivan
$container->setAction(new InNestedArray('ivan'))->executeAction();
// true: strtolower to values before compare
$container->setAction((new InNestedArray('ivan'))->setValueMutator(function($value){
if (!is_string($value)) {
return $value;
}
return strtolower($value);
}))->executeAction();
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\ExtractRandomItems;
$array = [
'a',
'b',
'c',
'd',
'e',
'f'
];
$container = new ArrayContainer($array);
$arrayNew = $container->setAction(new ExtractRandomItems(3))->executeAction();
$a1 = ['first' => 1, 'second' => 2];
$ar = array_merge($a1, ['second' => 22])
The result is:
$ar == ['first' => 1, 'second' => 22];
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\Concat;
$a1 = ['first' => 1, 'second' => 2];
$container = new ArrayContainer($a1);
$result = $container->setAction(new Concat())->executeAction(['second' => 22]);
The result is:
$ar == ['first' => 1, 'second' => 2, 22];
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\GetIntegerNumbersNotInSequence;
$array = [5, 1, 7, 8];
$container = new ArrayContainer($array);
$result = $container->setAction(new GetIntegerNumbersNotInSequence())->executeAction();
// result is:
[2,3,4,6] == $result;
use AndyDune\ArrayContainer\ArrayContainer;
use AndyDune\ArrayContainer\Action\FindMaxFloatValue;
$container = new ArrayContainer(['- 1', -2.56, 10, ' 1 1 ']);
$result = $container->setAction(new FindMaxFloatValue())->executeAction();
$this->assertEquals(11, $result);
// result is:
11 == $result;
use AndyDune\ArrayContainer\Path;
$arr = [
'key1' => 'bum',
'key2' => ['key21' => [
'key211' => 'mub'
]],
];
// To get value with key `key211` you need:
$arr['key2']['key21']['key211'] // mub
// with Path
$arrObject = new Path($arr);
(string)$arr->key2->key21->key211; // mub
use AndyDune\ArrayContainer\Path;
$arrObject = new Path($arr);
$arr->key2->key21->key211 = 'bum';
(string)$arr->key2->key21->key211; // 'bum'
$arr->key2->key21->noExist_id->getValue(); // null
use AndyDune\ArrayContainer\Builder;
use AndyDune\ArrayContainer\BuilderStrategy\MultilineTextToAssociatedArray;
$sourceText = '
one => two
three
=> four
';
$expectResult = [
'one' => 'two',
'four',
'three' => null
];
$builder = new Builder($sourceText, new MultilineTextToAssociatedArray('=>'));
// result is
$expectResult == $builder->execute();
use AndyDune\ArrayContainer\Builder;
use AndyDune\ArrayContainer\BuilderStrategy\MultilineTextAsJsonToAssociatedArray;
$text = '
{
"one":"two",
"two" : 2,
"three":null
}
';
$expectResult = [
'one' => 'two',
'two' => 2,
'three' => null
];
$builder = new Builder($text, new MultilineTextAsJsonToAssociatedArray());
// result is
$expectResult == $builder->execute();