Convert a Multidimensional Array Into Single Dimension
Tags: Array, class, PHP, Programming
RayArray is a array utility class.
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
* array into a single dimension array ready to store into sql table/ flat file.
*
* methods available are
* - shorten() - static
* - unshorten() - static
* - subarray() - static
*
* @package raynux
* @subpackage raynux.lab.array
* @version 1.0
* @author Md. Rayhan Chowdhury
* @email ray@raynux.com
* @website www.raynux.com
* @license GPL
*/
class RayArray{
/**
* Shorten an multidimensional array into a single dimensional array concatenating all keys with separator.
*
* @example array('country' => array(0 => array('name' => 'Bangladesh', 'capital' => 'Dhaka')))
* to array('country.0.name' => 'Bangladesh', 'country.0.capital' => 'Dhaka')
*
* @param array $inputArray, arrays to be marged into a single dimensional array
* @param string $path, Default Initial path
* @param string $separator, array key path separator
* @return array, single dimensional array with key and value pair
* @access public
* @static
*/
static public function shorten(array $inputArray, $path = null, $separator = "."){
$data = array();
if (!is_null($path)) {
$path = $path . $separator;
}
if (is_array($inputArray)) {
foreach ($inputArray as $key => &$value) {
if (!is_array($value)) {
$data[$path . $key] = $value;
} else {
$data = array_merge($data, self::shorten($value, $path . $key, $separator));
}
}
}
return $data;
}
/**
* Unshorten a single dimensional array into multidimensional array.
*
* @example array('country.0.name' => 'Bangladesh', 'country.0.capital' => 'Dhaka')
* to array('country' => array(0 => array('name' => 'Bangladesh', 'capital' => 'Dhaka')))
*
* @param array $data data to be converted into multidimensional array
* @param string $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 ){
$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($value)? self::unshorten($value, $separator) : $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')),
* 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 '.'
* @return mixed and return NULL if not found.
* @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)) {
return self::subarray($keys[1], $data[$keys[0]], $separator);
}
}
}
}
?>
Please drop me a line if you find this class useful or you have a different opinion.
« Http Client Class for PHP development
I-Blood profile badge ready to use »
Comments
Pingback from Yahoo! Beta Testing Blog Search Application Sites – Best Paying Affiliate Programs
Time: July 11, 2009, 1:15 am
[...] Convert a Multidimensional Array Into Single Dimension | Rayhan's … [...]
Comment from alex
Time: July 15, 2009, 7:08 pm
This is a great script. thank you …
Comment from Igor
Time: January 25, 2010, 7:20 pm
Thank U! Yr class is very useful.
Comment from ADRIAN Martinez Buleo
Time: July 14, 2010, 3:22 pm
THIS IS A GREAT SCRIPT!! I am a spanish programmer. I have a problem. I have a muli-dimensional array, variable because it comes from a sql realtional consult. Well, my question is: ¿How can I present all the information INTO A GRID? There are rows that I have to repeat when I show the information. Now there is an single array, but not a complete Grid. ¿Have you a solution? I need it. Thank very much and congratulations for your code.
Comment from western belt
Time: November 16, 2010, 2:10 pm
Wow this is what I’ve been looking for! I’ve been trying to figure out how to do this in a long time now. Thank you!
Comment from taos moccasins
Time: November 16, 2010, 2:13 pm
This script really is helping out a lot. I may use this for my windows registry too
Comment from Yader
Time: June 4, 2011, 2:02 am
Great scrips!!!Exactly what i was looking for
Comment from ankit
Time: January 21, 2012, 2:03 pm
thanks a lot u saved my time.
Pingback from nuclear bomb » Self-Assembly of Chiral DNA Nanotubes
Time: July 6, 2009, 3:03 am
[...] Convert a Multidimensional Array Into Single Dimension | Rayhan's … [...]