Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions src/Array/ArrayMulti.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function only(array $array, array|string $keys): array
return $result;
}


/**
* Collapses a multidimensional array into a single-dimensional array.
*
Expand All @@ -48,13 +47,12 @@ public static function collapse(array $array): array
$results = [];
foreach ($array as $values) {
if (is_array($values)) {
$results = array_merge($results, $values);
array_push($results, ...$values);
}
}
return $results;
}


/**
* Determine the depth of a multidimensional array.
*
Expand All @@ -81,7 +79,6 @@ public static function depth(array $array): int
return $depth + 1; // zero-based => plus one
}


/**
* Recursively flatten a multidimensional array to a specified depth.
*
Expand Down Expand Up @@ -112,7 +109,6 @@ public static function flatten(array $array, float|int $depth = \INF): array
return $result;
}


/**
* Flatten the array into a single level but preserve keys.
*
Expand All @@ -132,7 +128,6 @@ public static function flattenByKey(array $array): array
);
}


/**
* Recursively sort a multidimensional array by keys/values.
*
Expand Down Expand Up @@ -165,7 +160,6 @@ public static function sortRecursive(array $array, int $options = \SORT_REGULAR,
return $array;
}


/**
* Return the first item in a 2D array, or single-dim array, depending on usage.
* If a callback is provided, return the first item that matches the callback.
Expand All @@ -190,7 +184,6 @@ public static function first(array $array, ?callable $callback = null, mixed $de
return $default;
}


/**
* Return the last item in a 2D array or single-dim array, depending on usage.
* If a callback is provided, return the last item that matches the callback.
Expand All @@ -210,7 +203,6 @@ public static function last(array $array, ?callable $callback = null, mixed $def
return static::first(array_reverse($array, true), $callback, $default);
}


/**
* Filter a 2D array by a single key's comparison (like "where 'age' between 18 and 65").
*
Expand All @@ -227,7 +219,6 @@ public static function between(array $array, string $key, float|int $from, float
&& compare($item[$key], $to, '<='));
}


/**
* Filter a 2D array by a custom callback function on each row.
*
Expand All @@ -248,7 +239,6 @@ public static function whereCallback(array $array, ?callable $callback = null, m
return array_filter($array, fn ($item, $index) => $callback($item, $index), \ARRAY_FILTER_USE_BOTH);
}


/**
* Filter a 2D array by a single key's comparison (like "where 'age' > 18").
*
Expand Down Expand Up @@ -293,7 +283,6 @@ public static function chunk(array $array, int $size, bool $preserveKeys = false
return array_chunk($array, $size, $preserveKeys);
}


/**
* Apply a callback to each row in the array, optionally preserving keys.
*
Expand All @@ -315,7 +304,6 @@ public static function map(array $array, callable $callback): array
return $results;
}


/**
* Execute a callback on each item in the array, returning the original array.
*
Expand All @@ -339,7 +327,6 @@ public static function each(array $array, callable $callback): array
return $array;
}


/**
* Reduce an array to a single value using a callback function.
*
Expand All @@ -361,7 +348,6 @@ public static function reduce(array $array, callable $callback, mixed $initial =
return $accumulator;
}


/**
* Check if the array (of rows) contains at least one row matching a condition
*
Expand All @@ -379,7 +365,6 @@ public static function some(array $array, callable $callback): bool
return false;
}


/**
* Determine if all rows in a 2D array pass the given truth test.
*
Expand All @@ -400,7 +385,6 @@ public static function every(array $array, callable $callback): bool
return true;
}


/**
* Determine if the array contains a given value or if a callback function
* returns true for at least one element.
Expand All @@ -426,7 +410,6 @@ public static function contains(array $array, mixed $valueOrCallback, bool $stri
return in_array($valueOrCallback, $array, $strict);
}


/**
* Return a new array with all duplicate rows removed.
*
Expand Down Expand Up @@ -459,7 +442,6 @@ public static function unique(array $array, bool $strict = false): array
return $results;
}


/**
* Return an array with all values that do not pass the given callback.
*
Expand Down Expand Up @@ -487,7 +469,6 @@ public static function reject(array $array, mixed $callback = true): array
return array_filter($array, fn ($row) => $row != $callback);
}


/**
* Partition the array into two arrays [passed, failed] based on a callback.
*
Expand Down Expand Up @@ -516,7 +497,6 @@ public static function partition(array $array, callable $callback): array
return [$passed, $failed];
}


/**
* Skip the first $count items of the array and return the remainder.
*
Expand All @@ -532,7 +512,6 @@ public static function skip(array $array, int $count): array
return array_slice($array, $count, null, true);
}


/**
* Skip rows while the callback returns true; once false, keep the remainder.
*
Expand Down Expand Up @@ -562,7 +541,6 @@ public static function skipWhile(array $array, callable $callback): array
return $result;
}


/**
* Skip rows until the callback returns true, then keep the remainder.
*
Expand All @@ -582,7 +560,6 @@ public static function skipUntil(array $array, callable $callback): array
return static::skipWhile($array, fn ($row, $key) => !$callback($row, $key));
}


/**
* Calculate the sum of an array of values, optionally using a key or callback to extract the values to sum.
*
Expand Down Expand Up @@ -613,7 +590,6 @@ public static function sum(array $array, string|callable|null $keyOrCallback = n
return $total;
}


/**
* Filter rows where "column" matches one of the given values.
*
Expand All @@ -632,7 +608,6 @@ public static function whereIn(array $array, string $key, array $values, bool $s
);
}


/**
* Filter rows where "column" does NOT match one of the given values.
*
Expand All @@ -651,7 +626,6 @@ public static function whereNotIn(array $array, string $key, array $values, bool
);
}


/**
* Filter rows where a column is null.
*
Expand All @@ -671,7 +645,6 @@ public static function whereNull(array $array, string $key): array
);
}


/**
* Filter rows where a column is not null.
*
Expand All @@ -687,7 +660,6 @@ public static function whereNotNull(array $array, string $key): array
return array_filter($array, fn ($row) => isset($row[$key]));
}


/**
* Group a 2D array by a given column or callback.
*
Expand Down Expand Up @@ -728,7 +700,6 @@ public static function groupBy(array $array, string|callable $groupBy, bool $pre
return $results;
}


/**
* Sort a 2D array by a specified column or using a callback function.
*
Expand Down Expand Up @@ -761,7 +732,6 @@ public static function sortBy(
return $array;
}


/**
* Sort a 2D array by a specified column or using a callback function, in descending order.
*
Expand All @@ -776,4 +746,47 @@ public static function sortByDesc(array $array, string|callable $by, int $option
{
return static::sortBy($array, $by, true, $options);
}

/**
* Transpose a 2D array (matrix).
*
* This method takes a matrix (2D array) and returns a new matrix where the
* rows are converted into columns and vice versa. If the input matrix is empty,
* it returns an empty array.
*
* @param array $matrix The matrix to transpose.
* @return array The transposed matrix.
*/
public static function transpose(array $matrix): array
{
if (empty($matrix)) {
return [];
}
$keys = array_keys(current($matrix));
$results = array_fill_keys($keys, []);

foreach ($matrix as $row) {
foreach ($row as $col => $value) {
$results[$col][] = $value;
}
}
return $results;
}

public static function pluck(array $array, string $column, ?string $indexBy = null): array
{
$results = [];
foreach ($array as $row) {
if (!is_array($row) || !array_key_exists($column, $row)) {
continue;
}
$value = $row[$column];
if ($indexBy !== null && array_key_exists($indexBy, $row)) {
$results[$row[$indexBy]] = $value;
} else {
$results[] = $value;
}
}
return $results;
}
}
Loading