PHP code example of belca / support-array
1. Go to this page and download the library: Download belca/support-array 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/ */
belca / support-array example snippets
use Belca\Support\Arr;
// Determine whether an array is filled with integer keys
$result = Arr::isIntKeys([1, 2, 3, 4, 5]); // Output: true
$result = Arr::isIntKeys([1, 2, 'three' => 3, 4, 5]); // Output: false
// Check whether the first and last key of an array are integers.
$result = Arr::isFirstLastWithIntKeys([1, 2, 3, 4, 5]); // Output: true
$result = Arr::isFirstLastWithIntKeys([1, 2, 3, 4, 'five' => 5]); // Output: false
// Take a first existing value of your array
$result = Arr::firstExists(null, null, 'value'); // Output: 'value'
use Belca\Support\Arr;
// Your code
$array = Arr::trim($array); // or another function
// or
$array = \Belca\Support\Arr::trim($array); // using the full path
$source = [1, 2, 3, 4, 5, 6];
$array = [6, 7, 8, 9, 10, 11, 12];
Arr::concatArray($source, $array);
// Output $source: [6, 7, 8, 9, 10, 11, 12];
$source = [1, 2, 3, 4, 5, 6];
$array = [6 => 6, 7, 8, 9, 10, 11, 12];
Arr::concatArray($source, $array);
// Output $source: [1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10, 11, 12];
$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];
Arr::concatArray($source, $newValues);
// Output $source: ['key1' => 1, 'key2' => 3, 'key3' => 4];
$source = ['key1' => 1, 'key2' => 2];
$newValues = ['key2' => 3, 'key3' => 4];
Arr::concatArray($source, $newValues, false);
// Output $source: ['key1' => 1, 'key2' => 2, 'key3' => 4];
$result = Arr::firstExists(null, null, false, 0, '', true); // Output: false
$result = Arr::firstExists(null, null, 0, '', 'value', true, []); // Output: 0
$result = Arr::firstExists(null, null, [], '', 'value', true); // Output: []
$result = Arr::firstNotEmpty(null, null, false, 0, '', true); // Output: true
$result = Arr::firstNotEmpty(null, null, false, 0, '', 'value', true, []); // Output: 'value'
$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isArrayWithIntKeys($array1); // true
$array2 = []; // false
$result2 = Arr::isArrayWithIntKeys($array2); // false, потому что пустой массив
$array3 = ['1' => 1, 2, 3, '4' => 4];
$result3 = Arr::isArrayWithIntKeys($array4); // true, потому что числа в строке преобразованы в integer
$array4 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result4 = Arr::isArrayWithIntKeys($array5); // false
$array1 = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isFirstLastWithIntKeys($array1); // true
$array2 = [];
$result2 = Arr::isFirstLastWithIntKeys($array2); // false, because the array is empty
$array3 = ['1' => 1, 2, 3, '4' => 4];
$result3 = Arr::isFirstLastWithIntKeys($array4); // true, because the number in the keys converted to the integer
$array4 = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result4 = Arr::isFirstLastWithIntKeys($array5); // true, because the first and the last keys are the integer
$array5 = [50 => 1, 'a2' => 3, 'a3' => 4, 'one' => 1];
$result5 = Arr::isFirstLastWithIntKeys($array6); // false, because the last key is the string
$normalArray = [1, 2, 3, 4, 5, 6, 7, 8, 10];
$result1 = Arr::isIntKeys($normalArray); // true
$badArray = [50 => 1, 'a2' => 3, 'a3' => 4, 0 => 1];
$result2 = Arr::isIntKeys($badArray); // false
$array = [5 => 1, 2, 3, 4, 5];
$last = Arr::last($array); // Output: 5
$source = [1, 2, 3, 'key1' => 1, 'key2' => 2, 'key3' => 3];
$array1 = [4, 5, 6];
$array2 = [1, 2, 3, 'key1' => 10];
Arr::pushArray($source, $array1, $array2);
// Output $source:
// [1, 2, 3, 'key1' => 10, 'key2' => 2, 'key3' => 3, 4, 5, 6, 1, 2, 3]
$array = [
1,
2,
3,
'four' => 4,
'five' => 5,
'matrix' => [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
7,
'eight' => 8,
9,
'symbols' => ['a', 'b', 'c'],
'object' => new stdClass(),
];
$result = Arr::removeArrays($array);
// Output: [
// 1,
// 2,
// 3,
// 'four' => 4,
// 'five' => 5,
// 7,
// 'eight' => 8,
// 9,
// 'object' => new stdClass(),
// ];
$array = [
1,
2,
3,
'four' => 4,
'five' => 5,
'matrix' => [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
7,
'eight' => 8,
9,
'symbols' => ['a', 'b', 'c'],
'object' => new stdClass(),
];
$result = Arr::removeArrays($array, true);
// Output: [1, 2, 3, 4, 5, 7, 8, 9, new stdClass()];
$array4 = [1, 2, null, '', [], new stdClass, false, 0];
$result = Arr::removeEmpty($array);
// Output: [1, 2, 5 => new stdClass];
$array = [
1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
-2,
'a3' => [
1,
2,
'a3.3' => [0, 1, 2, 3],
],
null,
'',
0,
false,
];
$result = Arr::removeEmptyRecurcive($array);
// Output:
// [
// 1 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4], 3 => 4],
// 2 => -2,
// 'a3' => [
// 0 => 1,
// 1 => 2,
// 'a3.3' => [0 => 1, 1 => 2, 2 => 3],
// ],
// ]
$array = [
1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
-2,
'a3' => [
1,
2,
'a3.3' => [0, 1, 2, 3],
],
null,
'',
0,
false,
];
$result = Arr::removeEmptyRecurcive($array, false);
// Output:
// [
// 1 => [1, 2, 3 => [1, 2, 3, 4], 4 => 4],
// 2 => -2,
// 'a3' => [
// 0 => 1,
// 1 => 2,
// 'a3.3' => [1 => 1, 2 => 2, 3 => 3],
// ],
// ]
$array = [1, 2, null, '', [], new stdClass, false, 0];
$result = Arr::removeNotScalar($array);
// Output: [0 => 1, 1 => 2, 3 => '', 6 => false, 7 => 0];
$array = [1, 2, null, '', [], new stdClass, false, 0];
$result = Arr::removeNull($array);
// Output: [1, 2, 3 => '', [], new stdClass, false, 0];
$array = [
1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
-2,
4 => [
1 => 1,
2 => 2,
'a3.3' => [0, 1, 2, 3],
],
null,
'',
0,
false,
];
$result = Arr::removeNullRecurcive($array);
// Output:
// [
// 0 => [0 => 1, 1 => 2, 2 => [1, 2, 3, 4, []], 3 => 4, 4 => ''],
// 1 => -2,
// 2 => [
// 1 => 1,
// 2 => 2,
// 'a3.3' => [0 => 1, 1 => 2, 2 => 3],
// ],
// 3 => '',
// 4 => 0,
// 5 => false,
// ]
$array = [
1 => [1, 2, 3 => [1, 2, 3, 4, [], null], 4, ''],
-2,
4 => [
1 => 1,
2 => 2,
'a3.3' => [0, 1, 2, 3],
],
null,
'',
0,
false,
];
$result = Arr::removeNullRecurcive($array, false);
// Output:
// [
// 1 => [1, 2, 3 => [1, 2, 3, 4, []], 4 => 4, 5 => ''],
// 2 => -2,
// 4 => [
// 1 => 1,
// 2 => 2,
// 'a3.3' => [1 => 1, 2 => 2, 3 => 3],
// ],
// 6 => '',
// 7 => 0,
// 8 => false,
// ]
$array = [
' value ',
'trim',
'one ',
' two',
' three ',
1,
2,
'string',
null,
[' no trim '],
];
$result = Arr::trim($array);
// Output: ['value', 'trim', 'one', 'two', 'three', 1, 2, 'string', null, [' no trim ']];
$array = [
1,
2,
3,
'four' => 4,
'five' => 5,
'matrix' => [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
],
7,
'eight' => 8,
9,
'symbols' => ['a', 'b', 'c'],
'object' => new stdClass(),
];
$output = Arr::unset($array, 0, 'four', 'eight', 4);
// or
$output = Arr::unset($array, [0, 'four', 'eight', 4]);
// or
$output = Arr::unset($array, [0, 'four'], ['eight', 4]);
// or
$output = Arr::unset($array, [0, 'four'], [['eight'], [4], []]);
// Output:
// [
// 1 => 2,
// 2 => 3,
// 'five' => 5,
// 'matrix' => [
// [1, 2, 3],
// [4, 5, 6],
// [7, 8, 9],
// ],
// 3 => 7,
// 'symbols' => ['a', 'b', 'c'],
// 'object' => new stdClass(),
// ]
$array = [
1, 2, 3, 4, 5, 'six' => 6, 'seven' => 7, 8, 9, 'ten' => 10, 11
];
Arr::unsetByReference($array, 0, 'six', 'ten', 'unknown');
// Output $array:
// [
// 1 => 2, 3, 4, 5, 'seven' => 7, 8, 9, 11
// ]
$array = [
1, 2, 3, 4, 5, 'six' => 6, 'seven' => 7, 8, 9, 'ten' => 10, 11
];
Arr::unsetByReference($array, [0, 1], ['six', 'ten', 'unknown']);
// Output $array:
// [
// 2 => 3, 4, 5, 'seven' => 7, 8, 9, 11
// ]