| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Utility; |
| 6 |
|
| 7 |
final class Arr |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Get a partial array slice with start/end indexes. |
| 11 |
* |
| 12 |
* @param array $array the array |
| 13 |
* @param int $start the starting index (negative = count from backward) |
| 14 |
* @param null|int $end the ending index (negative = count from backward) |
| 15 |
* if is null, it returns a slice from $start to the end |
| 16 |
* |
| 17 |
* @return array array of all of the lines between the specified range |
| 18 |
*/ |
| 19 |
public static function getPartialByIndex(array $array, int $start = 0, ?int $end = null): array |
| 20 |
{ |
| 21 |
$count = \count($array); |
| 22 |
|
| 23 |
// make $end set |
| 24 |
$end ??= $count; |
| 25 |
|
| 26 |
// make $start non-negative |
| 27 |
if ($start < 0) { |
| 28 |
$start += $count; |
| 29 |
|
| 30 |
if ($start < 0) { |
| 31 |
$start = 0; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// make $end non-negative |
| 36 |
if ($end < 0) { |
| 37 |
$end += $count; |
| 38 |
|
| 39 |
if ($end < 0) { |
| 40 |
$end = 0; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// make the length non-negative |
| 45 |
return \array_slice($array, $start, max(0, $end - $start)); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Determines whether the array is associative. |
| 50 |
* |
| 51 |
* @param array $arr the array |
| 52 |
* |
| 53 |
* @return bool `true` if the array is associative, `false` otherwise |
| 54 |
*/ |
| 55 |
public static function isAssociative($arr): bool |
| 56 |
{ |
| 57 |
foreach ($arr as $key => $value) { |
| 58 |
if (\is_string($key)) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
} |