Ответы пользователя по тегу Массивы
  • Десятимерный массив из строки (JS)

    iStyx
    @iStyx
    Just proof of concept:

    <?php
    
    $str = "ЭНИ-601[2к[10Вт;20Вт];4к[10Вт;20Вт;30Вт];8к[70Вт;80Вт]];";
    
    $str = preg_replace("#([а-яА-Я\d\-]+)#u","\"\$1\"",$str);
    $str = str_replace(array("[","]"),array(":{","}"),$str);
    $str = preg_replace("#\{([^{};]+?(;[^}]+?)*?)\}#u","[\$1]",$str);
    $str = str_replace(";",",",$str);
    $str = '{'.substr($str,0,-1).'}';
    
    print_r(json_decode($str,true));
    echo PHP_EOL;
    
    


    Array
    (
        [ЭНИ-601] => Array
            (
                [2к] => Array
                    (
                        [0] => 10Вт
                        [1] => 20Вт
                    )
    
                [4к] => Array
                    (
                        [0] => 10Вт
                        [1] => 20Вт
                        [2] => 30Вт
                    )
    
                [8к] => Array
                    (
                        [0] => 70Вт
                        [1] => 80Вт
                    )
    
            )
    )
    
    Ответ написан
    2 комментария