0

I try to convert a string containing shedule to an array

For exemple

convert("{{09:00,10:00},{18:00,19:00}}") // returns [['09:00','10:00'],['18:00','19:00']]

Is there a way more elegant than this one :

    function convert($shedules) {
            $shedules = explode('},{', $horaires);
            $shedules = array_map(function ($shedule) {
                return explode(',', str_replace(['{', '}'], '', $shedule));
            }, $shedules);

          return $shedules
    }
Gautier
  • 1,066
  • 1
  • 13
  • 24
  • How do you get the string? Can you not get valid JSON with quoted data? – AbraCadaver Oct 13 '21 at 15:01
  • No I can't it is old data stored in database – Gautier Oct 13 '21 at 15:05
  • Could you add a few more examples of the data format, so that we know what the constraints are? For instance: is it always exactly 2 pairs of values, or a variable length list of pairs, or completely variable? Are the values always times in HH:MM format, or are there other possibilities? (If you have any unit tests, the samples to show us will be the same ones that make really good test cases. :)) – IMSoP Oct 13 '21 at 15:13
  • The values are always HH:MM but it is a variable list of pairs – Gautier Oct 13 '21 at 15:34

0 Answers0