php flatten
v4.0.1
一个实用函数,主要将多维数组和可遍历元素展平为一维数组,保留键并将它们与可自定义的分隔符连接起来,以形成最终数组中的完全限定键。
composer require sarhan/php-flatten
实施例1
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 );
实施例2
自定义分隔符和初始前缀
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 );
实施例3
请注意,FQkey 中的前缀不会被分隔。如果需要分隔,则必须在前缀字符串后附加分隔符。
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 );
实施例4
数字键被视为关联键。
注意:可以使用标志来更改此行为。请参阅 FLAG_NUMERIC_NOT_FLATTENED
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 );
FLAG_NUMERIC_NOT_FLATTENED
使用数字(整数)键关闭展平值。
这些值将被包装在一个数组中(保留它们的键)并与父 FQK 关联。
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 );
顶级数字(整数)键也将返回到分配给传递的前缀的数组中。
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 );