mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
35 lines
890 B
PHP
35 lines
890 B
PHP
<?php
|
|
namespace App\Macros;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
final class To2dArrayWithDottedKeys
|
|
{
|
|
public function __invoke(): \Closure
|
|
{
|
|
return function ($prefix = '') {
|
|
/**
|
|
* @var $this Collection
|
|
*/
|
|
$iterator = new \RecursiveIteratorIterator(
|
|
new \RecursiveArrayIterator($this->toArray()),
|
|
\RecursiveIteratorIterator::SELF_FIRST
|
|
);
|
|
$path = [];
|
|
$flatArray = [];
|
|
|
|
foreach ($iterator as $key => $value) {
|
|
$path[$iterator->getDepth()] = $key;
|
|
|
|
if (!is_array($value)) {
|
|
$flatArray[
|
|
$prefix . implode('.', array_slice($path, 0, $iterator->getDepth() + 1))
|
|
] = $value;
|
|
}
|
|
}
|
|
|
|
return $flatArray;
|
|
};
|
|
}
|
|
}
|