2017 © Pedro Peláez
 

library php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

image

sarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  • Saturday, October 15, 2016
  • by AlaaSarhan
  • Repository
  • 0 Watchers
  • 2 Stars
  • 1,631 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 7 % Grown

The README.md

php-flatten

Latest Version Software License Total Downloads, (*1)

A utility function to mainly flatten multidimensional-arrays and traversables into a one-dimensional array, preserving keys and joining them with a customizable separator to from fully-qualified keys in the final array., (*2)

Installation

  composer require sarhan/php-flatten

Usage

Example 1, (*3)

use Sarhan\Flatten\Flatten;

$multiArray = [
    'say' => 'what',
    'hi' => [ 'de' => 'Hallo', 'es' => 'Hola' ]
];

/*
Flatten::__construct(
    string $separator = '.',
    string $prefix = '',
    int $flags = 0
)
*/
$flatten = new Flatten();

// Flatten::flattenToArray is provided for convinience. It internally
// calls Flatten::flatten and converts it's output, which is a 1-dimensional
// iterator, into a 1-dimensional array.
$flattened = $flatten->flattenToArray($multiArray);

// Flatten::unflattenToArray is provided for convinience. It internally
// calls Flatten::unflatten and converts it's output, which is a recursive
// generator structure, into a multi-dimensional array.
$unflattened = $flatten->unflattenToArray($flattened);

assert($flattened == [
    'say' => what
    'hi.de' => Hallo
    'hi.es' => Hola
]);

assert($unflattened == $multiArray);

Example 2, (*4)

Custom Separator and initial prefix, (*5)

use Sarhan\Flatten\Flatten;

$allowAccess = [
    'root' => false,
    'var' => [ 'log' => ['nginx' => true, 'apt' => false], 'www' => true ],
];

$flatten = new Flatten(
  '/',  // separator
  '/'   // prefix
);

$flattened = $flatten->flattenToArray($allowAccess);

$unflattened = $flatten->unflattenToArray($flattened);

assert($flatten == [
    '/root' => false,
    '/var/log/nginx' => true,
    '/var/log/apt' => false,
    '/var/www' => true
]);

assert($unflattened == $allowAccess);

Example 3, (*6)

Notice that the prefix will not be separated in FQkeys. If it should be separated, separator must be appeneded to the prefix string., (*7)

use Sarhan\Flatten\Flatten;

$api = [
    'category' => [ 'health' => 321, 'sport' => 769, 'fashion' => 888 ],
    'tag' => [ 'soccer' => 7124, 'tennis' => [ 'singles' => 9833, 'doubles' => 27127 ] ],
];

$flatten = new Flatten('/', 'https://api.dummyhost.domain/');

$flattened = $flatten->flattenToArray($api);

$unflattened = $flatten->unflattenToArray($flattened);

assert($flattened == [
    'https://api.dummyhost.domain/category/health' => 321,
    'https://api.dummyhost.domain/category/sport' => 769,
    'https://api.dummyhost.domain/category/fashion' => 888,
    'https://api.dummyhost.domain/tag/soccer' => 7124,
    'https://api.dummyhost.domain/tag/tennis/singles' => 9833,
    'https://api.dummyhost.domain/tag/tennis/doubles' => 27127
]);

assert($unflattened == $api);

Example 4, (*8)

Numeric keys are treated as associative keys., (*9)

Note: This behavior can be changed using flags. See FLAG_NUMERIC_NOT_FLATTENED, (*10)

use Sarhan\Flatten\Flatten;

$nutrition = [
    'nutrition',
    'fruits' => [ 'oranges', 'apple', 'banana' ],
    'veggies' => ['lettuce', 'broccoli'],
];

$flatten = new Flatten('-');

$flattened = $flatten->flattenToArray($nutrition);

$unflattened = $flatten->unflattenToArray($flattened);

assert($flattened == [
    '0' => 'nutrition',
    'fruits-0' => 'oranges',
    'fruits-1' => 'apple',
    'fruits-2' => 'banana',
    'veggies-0' => 'lettuce',
    'veggies-1' => 'broccoli'
]);

assert($unflattened == $nutrition);

Flags

FLAG_NUMERIC_NOT_FLATTENED, (*11)

Turns off flattening values with numeric (integer) keys., (*12)

Those values will be wrapped in an array (preserving their keys) and associated to the parent FQK., (*13)

use Sarhan\Flatten\Flatten;

$examples = [
    'templates' => [
      ['lang' => 'js', 'template' => "console.log('%s');"],
      ['lang' => 'php', 'template' => 'echo "%s";']
    ],
    'values' => [3 => 'hello world', 5 => 'what is your name?']
];

$flatten = new Flatten(
  '.',
  'examples.',
  Flatten::FLAG_NUMERIC_NOT_FLATTENED
);

$flattened = $flatten->flattenToArray($examples);

$unflattened = $flatten->unflattenToArray($flattened);

assert($flattened == [
    'examples.templates' => [
        [
            'lang' => 'js',
            'template' => 'console.log(\'%s\')';
        ],
        [
            'lang' => 'php',
            'template' => 'echo "%s"'
        ]
    ],
    'examples.values' => [
        3 => 'hello world',
        5 => 'what is your name?'
    ]
]);

assert($unflattened == $examples);

Top level numeric (integer) keys will also be returned into an array assigned to the passed prefix., (*14)

use Sarhan\Flatten\Flatten;

$seats = [
  'A1',
  'A2',
  'B1',
  'B2',
  '_reserved' => ['A1', 'B1'],
  '_blocked' => ['B2']
];

$flatten = new Flatten(
  '_',
  'seats',
  Flatten::FLAG_NUMERIC_NOT_FLATTENED
);

$flattened = $flatten->flattenToArray($seats);

$unflattened = $flatten->unflattenToArray($flattened);

assert($flattened == [
    'seats' => ['A1', 'A2', 'B1', 'B2'],
    'seats_reserved' => ['A1', 'B1'],
    'seats_blocked' => ['B2']
]);

assert($unflattened == $seats);

The Versions

15/10 2016

dev-master

9999999-dev https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten

15/10 2016

v1.1.1

1.1.1.0 https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten

15/10 2016

dev-issue_3_redundant_empty_array

dev-issue_3_redundant_empty_array https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten

15/10 2016

v1.1.0

1.1.0.0 https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten

15/10 2016

dev-issue_1_numeric_not_flattened

dev-issue_1_numeric_not_flattened https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten

27/09 2016

v1.0.0

1.0.0.0 https://github.com/AlaaSarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

  Sources   Download

LGPL-3.0

The Requires

  • php >=5.5

 

The Development Requires

by Alaa Sarhan

array arrays flatten