<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rayhan's blog (raynux.com) &#187; Array</title>
	<atom:link href="http://raynux.com/blog/tag/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://raynux.com/blog</link>
	<description>Rayhan's Personal Web Blog Site</description>
	<lastBuildDate>Mon, 11 Apr 2011 06:33:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Convert a Multidimensional Array Into Single Dimension</title>
		<link>http://raynux.com/blog/2009/06/28/convert-a-multidimensional-array-into-single-dimension/</link>
		<comments>http://raynux.com/blog/2009/06/28/convert-a-multidimensional-array-into-single-dimension/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:51:10 +0000</pubDate>
		<dc:creator>rayhan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Reference]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://raynux.com/blog/?p=127</guid>
		<description><![CDATA[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 &#60;?php /** * RayArray arrays utility [...]]]></description>
			<content:encoded><![CDATA[<p><strong>RayArray</strong> is a array utility class.</p>
<p>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.</p>
<p>File: <strong>rayarray.php</strong></p>
<pre><code>&lt;?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' =&gt; array(0 =&gt; array('name' =&gt; 'Bangladesh', 'capital' =&gt; 'Dhaka')))
 *          to array('country.0.name' =&gt; 'Bangladesh', 'country.0.capital' =&gt; '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 =&gt; &amp;$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' =&gt; 'Bangladesh', 'country.0.capital' =&gt; 'Dhaka')
 *          to array('country' =&gt; array(0 =&gt; array('name' =&gt; 'Bangladesh', 'capital' =&gt; '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 =&gt; $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' =&gt; array('Bangladesh', 'Srilanka', 'India', 'Pakistan')),
 *                  array('help' =&gt; 'help.php'),
 *                  'test' =&gt; 'value',
 *                  10 =&gt;
 *          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, &amp;$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);
      }
   }
}
}
?&gt;</code></pre>
<p>Please drop me a line if you find this class useful or you have a different opinion.</p>
]]></content:encoded>
			<wfw:commentRss>http://raynux.com/blog/2009/06/28/convert-a-multidimensional-array-into-single-dimension/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

