Outils pour utilisateurs

Outils du site


tips_informatiques:programmation:php:code

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
tips_informatiques:programmation:php:code [2009/07/31 16:13]
nico
tips_informatiques:programmation:php:code [2009/08/12 00:00] (Version actuelle)
Ligne 200: Ligne 200:
 } }
 </​code>​ </​code>​
 +
 +
 +
 +
 +
 +
 +====== to_multilevel_array ======
 +
 +<code php>
 +/**
 + * Build an array from a list of strings
 + ​* ​
 + * E.g: Array with the following strings:
 + ​* ​
 + * '​general_description[0][0][string]'​
 + * '​general_description[0][1][string]'​
 + * '​general_description[1][0][string]'​
 + ​* ​
 + * @param array $strings Array of (strings => value) pairs to merge into a multilevel array
 + * @param string $opening_char
 + * @param string $closing_char
 + */
 +public static function to_multilevel_array($strings,​ $opening_char = '​[',​ $closing_char = '​]'​)
 +{
 +    $array = array();
 +    ​
 +    foreach ($strings as $string => $value) ​
 +    {
 +        self :: set_next_level_array($array,​ $string, $value, $opening_char,​ $closing_char); ​  
 +    }
 +    ​
 +    return $array;
 +}
 +
 +private static function set_next_level_array(&​$container_array,​ $string, $value, $opening_char = '​[',​ $closing_char = '​]'​) ​   ​
 +{
 +    $key = self :: get_value_between_chars($string,​ 0, $opening_char,​ $closing_char);​
 +    $sub_string = substr($string,​ strpos($string,​ $closing_char) + 1);
 +    ​
 +    if(isset($sub_string) && strlen($sub_string) > 0)
 +    {
 +        if(isset($container_array[$key]))
 +        {
 +            $sub_array = $container_array[$key];​
 +        }
 +        else
 +        {
 +            $sub_array = array();
 +        }
 +        ​
 +        self :: set_next_level_array($sub_array,​ $sub_string,​ $value, $opening_char,​ $closing_char);​
 +    ​
 +        $container_array[$key] = $sub_array;
 +    }
 +    else
 +    {
 +        if(isset($container_array[$key]))
 +        {
 +            $container_array[$key] = array_merge($container_array[$key],​ $value);
 +        }
 +        else
 +        {
 +            $container_array[$key] = $value;
 +        }
 +    }
 +}
 +</​code>​
 +
 +
 +
 +
 +
 +
 +
 +
tips_informatiques/programmation/php/code.1249049598.txt.gz · Dernière modification: 2009/07/31 00:00 (modification externe)