Click to See Complete Forum and Search --> : Convert string to array


katten
09-19-2008, 03:16 PM
Heya guys i have spent the last few hours trying to find a good solution to my problem without any success so here it is!


I have the follow

$section = array(
'routes.foo.bar' => 'hi',
'beta.test' => 'hello world'
);
// And so on


what i need is a function that converts it to the following

$section = array(
'routes' => array(
'foo' => array(
'bar' => 'hi'
)
),
'beta' => array(
'test' => 'hello world'
)
);

FourCourtJester
09-19-2008, 03:20 PM
I guess the problem is that the conversion failed? Can you be a bit more specific?

katten
09-19-2008, 03:21 PM
I guess the problem is that the conversion failed? Can you be a bit more specific?
Maybe i was not clear enough i need a function that converts it for me

FourCourtJester
09-19-2008, 03:30 PM
Aah.

You could try something akin to this model:

foreach element in $section, split the element using '.' into a $temp array. create a new array, pass the first $temp element as the first level item and keep creating new arrays for as many $temp elements you have left.

Example - routes.foo.bar

$temp = split('.', $section[0]); //splits into routes , foo and bar
$newArray[0] = $temp[0]; //saves first level as 'routes'
$newArray[0][0] = $temp[1] //saves second level as 'foo'
$newArray[0][0][0] = $temp[2 //saves third level as 'bar'

This is just the first way off the top of my head.

Hope that helps.

NogDog
09-19-2008, 06:19 PM
You could use the dreaded eval() function:

<?php
$section = array(
'routes.foo.bar' => 'hi',
'beta.test' => 'hello world'
);

$newArray = array();
foreach($section as $keys => $value)
{
$keyString = "['" . str_replace('.', "']['", $keys) . "']";
eval('$newArray'.$keyString."='$value';");
}

echo "<pre>".print_r($newArray,1)."</pre>";

SyCo
09-19-2008, 07:09 PM
Not eval(), nooooooooooooooooooooooo!

actually, very nicely done :)

katten
09-20-2008, 02:51 AM
You could use the dreaded eval() function:

<?php
$section = array(
'routes.foo.bar' => 'hi',
'beta.test' => 'hello world'
);

$newArray = array();
foreach($section as $keys => $value)
{
$keyString = "['" . str_replace('.', "']['", $keys) . "']";
eval('$newArray'.$keyString."='$value';");
}

echo "<pre>".print_r($newArray,1)."</pre>";


Already thought of that but i dont want to use the eval function. I was studying the zf code last night and noticed its configuration file does exactly what i want but i don't really understand how.


<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Ini.php 8988 2008-03-21 22:55:57Z rob $
*/


/**
* @see Zend_Config
*/
require_once 'Zend/Config.php';


/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Config_Ini extends Zend_Config
{
/**
* String that separates nesting levels of configuration data identifiers
*
* @var string
*/
protected $_nestSeparator = '.';

/**
* Loads the section $section from the config file $filename for
* access facilitated by nested object properties.
*
* If the section name contains a ":" then the section name to the right
* is loaded and included into the properties. Note that the keys in
* this $section will override any keys of the same
* name in the sections that have been included via ":".
*
* If the $section is null, then all sections in the ini file are loaded.
*
* If any key includes a ".", then this will act as a separator to
* create a sub-property.
*
* example ini file:
* [all]
* db.connection = database
* hostname = live
*
* [staging : all]
* hostname = staging
*
* after calling $data = new Zend_Config_Ini($file, 'staging'); then
* $data->hostname === "staging"
* $data->db->connection === "database"
*
* The $options parameter may be provided as either a boolean or an array.
* If provided as a boolean, this sets the $allowModifications option of
* Zend_Config. If provided as an array, there are two configuration
* directives that may be set. For example:
*
* $options = array(
* 'allowModifications' => false,
* 'nestSeparator' => '->'
* );
*
* @param string $filename
* @param string|null $section
* @param boolean|array $options
* @throws Zend_Config_Exception
* @return void
*/
public function __construct($filename, $section = null, $options = false)
{
if (empty($filename)) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Filename is not set');
}

$allowModifications = false;
if (is_bool($options)) {
$allowModifications = $options;
} elseif (is_array($options)) {
if (isset($options['allowModifications'])) {
$allowModifications = (bool) $options['allowModifications'];
}
if (isset($options['nestSeparator'])) {
$this->_nestSeparator = (string) $options['nestSeparator'];
}
}

$iniArray = parse_ini_file($filename, true);
$preProcessedArray = array();
foreach ($iniArray as $key => $data)
{
$bits = explode(':', $key);
$thisSection = trim($bits[0]);
switch (count($bits)) {
case 1:
$preProcessedArray[$thisSection] = $data;
break;

case 2:
$extendedSection = trim($bits[1]);
$preProcessedArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
break;

default:
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
}
}

if (null === $section) {
$dataArray = array();
foreach ($preProcessedArray as $sectionName => $sectionData) {
if(!is_array($sectionData)) {
$dataArray = array_merge_recursive($dataArray, $this->_processKey(array(), $sectionName, $sectionData));
} else {
$dataArray[$sectionName] = $this->_processExtends($preProcessedArray, $sectionName);
}
}
parent::__construct($dataArray, $allowModifications);
} elseif (is_array($section)) {
$dataArray = array();
foreach ($section as $sectionName) {
if (!isset($preProcessedArray[$sectionName])) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
}
$dataArray = array_merge($this->_processExtends($preProcessedArray, $sectionName), $dataArray);

}
parent::__construct($dataArray, $allowModifications);
} else {
if (!isset($preProcessedArray[$section])) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Section '$section' cannot be found in $filename");
}
parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications);
}

$this->_loadedSection = $section;
}

/**
* Helper function to process each element in the section and handle
* the "extends" inheritance keyword. Passes control to _processKey()
* to handle the "dot" sub-property syntax in each key.
*
* @param array $iniArray
* @param string $section
* @param array $config
* @throws Zend_Config_Exception
* @return array
*/
protected function _processExtends($iniArray, $section, $config = array())
{
$thisSection = $iniArray[$section];

foreach ($thisSection as $key => $value) {
if (strtolower($key) == ';extends') {
if (isset($iniArray[$value])) {
$this->_assertValidExtend($section, $value);
$config = $this->_processExtends($iniArray, $value, $config);
} else {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Section '$section' cannot be found");
}
} else {
$config = $this->_processKey($config, $key, $value);
}
}
return $config;
}

/**
* Assign the key's value to the property list. Handle the "dot"
* notation for sub-properties by passing control to
* processLevelsInKey().
*
* @param array $config
* @param string $key
* @param string $value
* @throws Zend_Config_Exception
* @return array
*/
protected function _processKey($config, $key, $value)
{
if (strpos($key, $this->_nestSeparator) !== false) {
$pieces = explode($this->_nestSeparator, $key, 2);
if (strlen($pieces[0]) && strlen($pieces[1])) {
if (!isset($config[$pieces[0]])) {
$config[$pieces[0]] = array();
} elseif (!is_array($config[$pieces[0]])) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
}
$config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
} else {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Invalid key '$key'");
}
} else {
$config[$key] = $value;
}
return $config;
}

}

NogDog
09-20-2008, 05:00 AM
Perhaps it's time to step back a bit and explain what it is you are trying to accomplish. If this is a config file issue, perhaps you should be thinking in terms of an XML config file and the SimpleXML or DOM classes for parsing it into navigable objects, instead of a rather arbitrary and contrived mechanism to create an array?

NogDog
09-20-2008, 06:22 AM
Check this out:

<?php
/**
* fun with recursion
*/
function make_array($keys, $value)
{
$keyParts = explode('.', $keys);
$nextKey = array_pop($keyParts);
if(count($keyParts))
{
$temp = array($nextKey => $value);
return make_array(implode('.', $keyParts), $temp);
}
return array($nextKey => $value);
}

$section = array(
'routes.foo.bar' => 'hi',
'routes.foo.test' => 'bye',
'beta.test' => 'hello world'
);

$result = array();
foreach($section as $key => $value)
{
$result = array_merge_recursive($result, make_array($key, $value));
}

echo "<pre>".print_r($result,1)."</pre>";

Output:

Array
(
[routes] => Array
(
[foo] => Array
(
[bar] => hi
[test] => bye
)

)

[beta] => Array
(
[test] => hello world
)

)

bokeh
09-20-2008, 08:53 AM
Already thought of that but i dont want to use the eval function.Why not?