I have wrapped three static functions for converting multidimensional configuration data. It is very much useful when working with configuration data. I have used this class in one of my project to store arbitrary configuration data just like windows registry data.
File: rayarray.php
<?php
/**RayArray arrays utility class
* *
* This class provides configuration array handling funcionalities,
* may be usefull when dealing with configuration data. *
*
* Usage: using this class you can convert a multidimensional configuration
sql table/ flat file. * * methods available are * - shorten() - static * - unshorten() - static * - subarray() - static * array into a single dimension array ready to store into
raynux.com * @website
* @package raynux * @subpackage raynux.lab.array * @version 1.0 * @author Md. Rayhan Chowdhury * @email ray@ www.raynux.com * @license GPL */ class RayArray{ /**
array concatenating all keys with separator. * * @example array('country' => array(0 => array('name' => '
* Shorten an multidimensional array into a single dimensionalBangladesh', 'capital' => 'Dhaka'))) * to array('country.0.name' => 'Bangladesh', 'country.0.capital' => 'Dhaka') *
@param string $separator, array key path separator * @return array, single dim
* @param array $inputArray, arrays to be marged into a single dimensional array * @param string $path, Default Initial path *ensional array with key and value pair * @access public * @static */ static public function shorten(array $inputArray, $path = null, $separator = "."){ $data = array(); if (!is_null($path)) {
} else { $data =
$path = $path . $separator; } if (is_array($inputArray)) { foreach ($inputArray as $key => &$value) { if (!is_array($value)) { $data[$path . $key] = $value; array_merge($data, self::shorten($value, $path . $key, $separator)); } } } return $data; } /** * Unshorten a single dimensional array into multidimensional array. *
aram array $data data to be converted into multidimensional array * @param string
* @example array('country.0.name' => 'Bangladesh', 'country.0.capital' => 'Dhaka') * to array('country' => array(0 => array('name' => 'Bangladesh', 'capital' => 'Dhaka'))) * * @p$separator key path separator * @return array multi dimensional array * @access public * @static */ static public function unshorten($data, $separator = '.'){ $result = array(); foreach ($data as $key => $value){ if(strpos($key, $separator) !== false ){
lue)? self::unshorten($value, $separator) :
$str = explode($separator, $key, 2); $result[$str[0]][$str[1]] = $value; if(strpos($str[1], $separator)){ $result[$str[0]] = self::unshorten($result[$str[0]], $separator); } }else{ $result[$key] = is_array($va$value; } } return $result; } /** * Get part of array from a multidimensional array specified by concatenated keys path. * * @example * path = "0.name" * data = * array( * array('name' => array('Bangladesh', 'Srilanka', 'India', 'Pakistan')),
' * @return mixed and return NULL if not found.
* array('help' => 'help.php'), * 'test' => 'value', * 10 => * false) * will return array('Bangladesh', 'Srilanka', 'India', 'Pakistan') * @param string $path * @param array $data * @param string $separator path separator default '. * @access public * @static */ static public function subarray($path, &$data, $separator = '.') { if (strpos($path, $separator) === false) { if (isset($data[$path])) { return $data[$path]; } } else { $keys = explode($separator, $path, 2); if (array_key_exists($keys[0], $data)) {
Please drop me a line if you find this class useful or you have a different opinion.
1 comments:
MySql Hosting India
IndoUs Hosting is the leading MySql Hosting companies in India. The company provides dedicated as well as shared hosting services of MySql hosting. If you are looking for guaranteed MySql Hosting India, we can be the best option for you.
Post a Comment