There is data that lies in the file file.conf . I read it into a two-dimensional array, then using the explode method explode get what I need it. But I need to return it in the corrected form, which I can’t do. The code itself:

 $array = file('Config.conf'); function explodearray() { for ($i = 0; $i <= count($k); $i++) { for ($s = 0; $s <= count($k); $s++) { $k[$i][$s] = explode('%',$k[$i][$s]); echo $k[2][2]; } } return $k; echo "done"; } explodearray($array);` 

file.conf :

 Plate_1%59 DI_1%true%Switch 1%false%DO_1%2 DI_2%true%switch 2%true%DO_2%2` 

On exit, I have to get a modified array. Be kind, tell me something.

  • Explain what kind of "that corrected view" in which you want to return it (an array?) - S. Pronin
  • one
    Hello! But you can not use the standard format, such as JSON, instead of self-made horror? Then both reading and writing will be carried out literally in one line. - Ipatiev
  • The corrected view is a string broken by the method of explode ('%', $ line); - Dima Balovnev
  • The fact of the matter is that json is nothing. This is the configuration file that I have to read and enter into it. - Dima Balovnev

1 answer 1

Maybe this code will suit you:

 <?php /** * Функция для "разбивания" строк * @param array $result */ function explodearray(array $arr) { $result = array(); $result2 = array(); $result3 = array(); foreach ($arr as $string) { //Можно так (Вар 1) $result[] = explode("%", $string); //Или так (Вар 2) $parameters = array_chunk(explode("%", $string), 2); foreach ($parameters as $one) { if (isset($one[0]) AND isset($one[1])) { $result2[$one[0]] = $one[1]; } } //Или так (Вар 3) $result3 = array_merge($result3, explode("%", $string)); } echo "Вариант 1: "; print_r($result); echo "Вариант 2: "; print_r($result2); echo "Вариант 3: "; print_r($result3); return $result; } /** * Функция file() получает содержимое файла в виде массива строк. * Возьмем Ваш тестовый пример содержимого и завернем его в массив */ $file_conf = array( "Plate_1%59", "DI_1%true%Switch 1%false%DO_1%2", "DI_2%true%switch 2%true%DO_2%2" ); explodearray($file_conf);