Skip to content

Commit

Permalink
New method ArrayHelper::simpleArray() to simplify a multidimensiona…
Browse files Browse the repository at this point in the history
…l array
  • Loading branch information
ElGigi committed Oct 26, 2022
1 parent ced1768 commit b2df4e3
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.9.0] - 2022-10-26

### Added

- New method `ArrayHelper::simpleArray()` to simplify a multidimensional array

## [1.8.0] - 2022-03-18

### Added
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ Many PHP functions used in the Berlioz framework, which you can use in your deve

Traverse array with path and set value.

- `b_array_simple(array $array, ?string $prefix = null): array`

Simplify a multidimensional array to simple.

## File

- `b_human_file_size($size, int $precision = 2): string`
Expand Down
31 changes: 31 additions & 0 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use ArrayObject;
use Closure;
use Exception;
use InvalidArgumentException;
use SimpleXMLElement;
use Traversable;
Expand Down Expand Up @@ -159,6 +160,7 @@ public static function column(array $array, $column_key, $index_key = null): arr
* @param string|null $rootName
*
* @return SimpleXMLElement
* @throws Exception
*/
public static function toXml($array, ?SimpleXMLElement $root = null, ?string $rootName = null): SimpleXMLElement
{
Expand Down Expand Up @@ -338,4 +340,33 @@ public static function traverseSet(iterable &$mixed, string $path, $value): bool

return true;
}

/**
* Transform multidimensional array to simple level.
*
* @param array $array
* @param string|null $prefix
*
* @return array
*/
public static function simpleArray(array $array, ?string $prefix = null): array
{
$metaData = [];

foreach ($array as $key => $value) {
// Prefix key if necessary
if (null !== $prefix) {
$key = $prefix . '.' . $key;
}

if (is_array($value)) {
$metaData = array_merge($metaData, self::simpleArray($value, $key));
continue;
}

$metaData[$key] = $value;
}

return $metaData;
}
}
13 changes: 13 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ function b_array_traverse_set(&$mixed, string $path, $value): bool
return ArrayHelper::traverseSet($mixed, $path, $value);
}

/**
* Simplify multi-dimensional array.
*
* @param array $array
* @param string|null $prefix
*
* @return array
*/
function b_array_simple(array $array, ?string $prefix = null): array
{
return ArrayHelper::simpleArray($array, $prefix = null);
}


///////////////////
/// FILE HELPER ///
Expand Down
36 changes: 36 additions & 0 deletions tests/ArrayHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,40 @@ public function testTraverseSet()
$this->assertFalse(ArrayHelper::traverseSet($tArray, 'foo.bar.foo', 'bar'));
$this->assertTrue(ArrayHelper::traverseSet($tArray, 'bar.foo', 'baz'));
}

public function testSimpleArray()
{
$arr = [
'foo' => 'bar',
'foo2' => [
'foo3' => ['foo4' => 'bar4'],
'foo5' => 'bar5',
'foo6' => [
'foo7' => 'bar7',
'foo8' => 'bar8',
],
],
];

$this->assertEquals(
[
'foo' => 'bar',
'foo2.foo3.foo4' => 'bar4',
'foo2.foo5' => 'bar5',
'foo2.foo6.foo7' => 'bar7',
'foo2.foo6.foo8' => 'bar8',
],
ArrayHelper::simpleArray($arr),
);
$this->assertEquals(
[
'prefix.foo' => 'bar',
'prefix.foo2.foo3.foo4' => 'bar4',
'prefix.foo2.foo5' => 'bar5',
'prefix.foo2.foo6.foo7' => 'bar7',
'prefix.foo2.foo6.foo8' => 'bar8',
],
ArrayHelper::simpleArray($arr, 'prefix'),
);
}
}

0 comments on commit b2df4e3

Please sign in to comment.