Perforce Chronicle 2012.2/486814
API Documentation

P4Cms_Filter_FlattenArray Class Reference

Flattens a multi-dimensional array using array notation for the keys to represent the hierarchy. More...

List of all members.

Public Member Functions

 filter ($input)
 Flatten multi-dimensional input array using array notation.

Protected Member Functions

 _flatten ($input, $prefix, array &$output=array())
 Recursively flatten the given input array.

Detailed Description

Flattens a multi-dimensional array using array notation for the keys to represent the hierarchy.

Copyright:
2011-2012 Perforce Software. All rights reserved
License:
Please see LICENSE.txt in top-level folder of this distribution.
Version:
2012.2/486814

Member Function Documentation

P4Cms_Filter_FlattenArray::_flatten ( input,
prefix,
array &$  output = array() 
) [protected]

Recursively flatten the given input array.

Parameters:
mixed$inputa input variable to flatten.
string$prefixthe key prefix to use.
array&$outputthe flattened output array (by reference).
Returns:
void
    {
        if (!is_array($input)) {
            $output[$prefix] = $input;
            return;
        }
        foreach ($input as $key => $value) {
            $key = $prefix . "[" . $key . "]";
            if (is_array($value)) {
                $this->_flatten($value, $key, $output);
            } else {
                $output[$key] = $value;
            }
        }
    }
P4Cms_Filter_FlattenArray::filter ( input)

Flatten multi-dimensional input array using array notation.

Parameters:
array$inputthe multi-dimensional input array to flatten.
Returns:
array the flattened output array.
    {
        // only flatten arrays.
        if (!is_array($input)) {
            throw new InvalidArgumentException("Cannot flatten. Value is not an array.");
        }
        
        $output = array();
        foreach ($input as $key => $value) {
            $this->_flatten($value, $key, $output);
        }

        return $output;
    }

The documentation for this class was generated from the following file: