Answer the question
In order to leave comments, you need to log in
Ten dimensional array from string (JS)
Hello. The task is as follows: the site has devices that can be in different versions. For example, the ENI-701 device can be 2-channel, 4-channel, 8-channel. In turn, the 2-channel ENI-701 can be 10 W, 20 W. 4-channel for 10, 20, 30 watts. Etc. For ease of perception:
cacoo.com/diagrams/v2Qko8L4XGhYKh22
The configuration of this device is set as follows:
var str = "ЭНИ-601[2к[10Вт;20Вт];4к[10Вт;20Вт;30Вт];8к[70Вт;80Вт]];"
Nesting can be even more (that is, ENI-601, 2-k, 10W can be in different versions). the maximum number of nestings reaches 10.
The question is: How to process this string to get arrays with elements:
How can this be implemented? Maybe I'm generally thinking in the wrong direction and is there a less perverted way?
arr[0] == "Эни-601";
arr[0][0] == "2к";
arr[0][1] == "4к";
arr[0][2] == "8к";
arr[0][0][0] == "10Вт";
arr[0][0][1] == "20Вт";
arr[0][1][0] == "10Вт";
arr[0][1][1] == "20Вт";
arr[0][1][2] == "30Вт";
arr[0][2][0] == "70Вт";
arr[0][2][1] == "80Вт";
Answer the question
In order to leave comments, you need to log in
It seems to me that you are deceiving us. For your task, you need not a ten-dimensional array, but a tree. If you really need a ten-dimensional array, then you are probably really into string theory.
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Вт
)
)
)
It is more convenient to implement a tree. The root is ENI-601, from it there are three branches 2k, 4k, 8k, from each of them - wattage. Or the second option is to separately store the options for cotton, hotelally 2k ... 8k, separate names and then reduce everything with pointers. But the tree version is more versatile.
The first thing that comes to mind is recursion by nesting levels. Input — "[", output — "]", loop iteration after ";"
Why not realizable? And how to implement a tree? If you can link to an example, I would be very grateful.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question