Minwork Array
  • Minwork Array
  • Common methods
    • has
    • get → getNestedElement
    • set → setNestedElement
    • remove
    • clone
    • getKeysArray
  • Object oriented methods
    • General information
  • Traversing array
    • Finding
    • Iterating
  • Manipulating array
    • Mapping
    • Filtering
    • Grouping
    • Sorting
    • Computations
    • Flattening
  • Validating array
    • check
    • isEmpty
    • isNested
    • isArrayOfArrays
    • isAssoc
    • isUnique
    • isNumeric
    • hasKeys
  • Utility methods
    • pack
    • unpack
    • createMulti
    • forceArray
    • getDepth
    • random
    • shuffle
    • nth
    • getFirstKey
    • getLastKey
    • getFirstValue
    • getLastValue
Powered by GitBook
On this page

Was this helpful?

  1. Utility methods

pack

Definition

Arr::pack(array $array): array

Description

Converts map of keys concatenated by dot and corresponding values to multidimensional array.

Inverse of unpack.

Examples

Let's use result array from example below.

$array = [
    'key1.key2.key3.foo' => 'test',
    'key1.key2.key3.bar' => 'test2',
    'key1.abc.0' => 'test3',
    'xyz' => 'test4',
    '0' => 'test5',
];

Arr::pack($array) -> 
[
    'key1' => [
        'key2' => [
            'key3' => [
                'foo' => 'test',
                'bar' => 'test2',
            ]
        ]
        'abc' => ['test3'],
    ],
    'xyz' => 'test4',
    'test5'
]

// Unpack is inverse operation to pack
$array2 = [
    'test',
    [
        'foo' => ['bar'],
        'a' => [
            'b' => 1
        ]
    ]
];

Arr::unpack(Arr::pack($array2)) === $array2 -> true
PrevioushasKeysNextunpack

Last updated 5 years ago

Was this helpful?