Stuck on trying to turn input into a string of certain format
<p>So I have a field in the db for delivery times, and it is of certain format (not my design for sure!):
Here are a couple of examples:</p>

<p>M, T, W, TH ; MTWTH 9 TO 4 CLOSED 12 TO 1</p>

<p>T, W, TH, F ; TTHF 9 TO 5 CLOSED 12 TO 2, W 9 TO 12</p>

<p>T, W, TH, F ; T 10 TO 7 CLOSED 2 TO 3, WTH 9 TO 6 CLOSED 1 TO 2, F 8 TO 5 CLOSED 1 TO 2</p>

<p>So basically, you have a list of days, separated by a comma, followed by semi-colon.
After semi-colon there is a list of days, not separated, and followed by delivery times including break times.
The problem is, delivery times can be different for each day, same for all days, or same for let's say Monday and tuesday, then different for Wednesday, and same for Thursday and Friday (but Thursday and Friday have different delivery times then Monday and Tuesday).
And if delivery times match for some days, those days should be grouped together, with delivery times listed after the group days, then other matched or single days listed after that. First day for which there are delivery hours specified must go first.</p>

<p>I know, the format is ridiculous and delivery days/times should be in a separate table linked by organization id, but it's the format they use now for some download files.
I hope to change it later, but for now...
I can't figure out how to transform input from 4 drop-downs (From, To, Closed From, Closed To) into that format.
Any of the days may not have closing times at all. At least one day must have delivery times specified (I check for that).</p>

<p>I of course got the first part (before semi-colon) - that's easy, after that I'm totally stuck.
If all days for which there's delivery had the same hours, I wouldn't have issues. But since it can be totally different from day to day, I don't know how to do it.</p>

<p>So this is the processing block so far:</p>

<p>$trimmed is the array of POST values that has been trimmed, </p>

<p>$trimmed['1Monday'] is Monday From, $trimmed['2Monday'] is Monday To, </p>

<p>$trimmed['3Monday'] is Monday Closed From, $trimmed['4Monday'] is Monday Closed To.</p>

<p>So  $M_times variable will have open/closing times for Monday. </p>

<p>$days_del array is used to later create a string that is the first part of required format.</p>

<p>I can get the first day with some times specified and I can try to catch any matches after that, but that isn't useful if first delivery day has different values from the rest, yet there are some matching values after that. I'm not even sure that I need to use anoither (numbered) days array or I can do it with just the main one with M-F for keys.</p>

<pre><code>&lt;?php

$days = array('M' =&gt; 'Monday', 'T' =&gt; 'Tuesday', 'W' =&gt; 'Wednesday', 'TH' =&gt; 'Thursday', 'F' =&gt; 'Friday');

$days_numbered = array(1 =&gt; 'M', 'T', 'W', 'TH', 'F');

$days_del = $matches = $full_list = array();

$days_delivery2 = '';

$count_no_del = 0;  # initialize count for days with no delivery
$times_6 = '';



foreach ($days as $k =&gt; $v){  #####  Beginning of days loop


if (isset($trimmed["1$v"]) &amp;&amp;  isset($trimmed["2$v"])){ #  If both closing and opening times have been specified

$days_del[] = $k;
${$k.'_times'} = $trimmed["1$v"] . ' TO ' . $trimmed["2$v"];

if ((!isset($trimmed["3$v"]) &amp;&amp; isset($trimmed["4$v"])) || (isset($trimmed["3$v"]) &amp;&amp; !isset($trimmed["4$v"]))){  #  If only beginning or only end of lunch(?) break has been specified

$errors[] = 'Delivery times for $v - please specify both start and end of closing time.';

}elseif (isset($trimmed["3$v"]) &amp;&amp; isset($trimmed["4$v"]){  #  If both beginning and end of lunch(?) breakh have been specified

${$k.'_times'} .= 'CLOSED' .  $trimmed["3$v"] . ' TO ' . $trimmed["4$v"];
}

    }elseif ((!isset($trimmed["1$v"]) &amp;&amp;  isset($trimmed["2$v"])) || (isset($trimmed["1$v"]) &amp;&amp;  !isset($trimmed["2$v"]))){  #  If only closing or only  opening time has been specified

$errors[] = 'Delivery times for $v - please specify both start and end of delivery time.';
${$k.'_times'} = NULL;

    }elseif (!isset($trimmed["1$v"]) &amp;&amp;  !isset($trimmed["2$v"])){  # No delivery times specified for this day

${$k.'_times'} = NULL;
$count_no_del++;

    }

$full_list["$k"] = ${$k.'_times'};

}  #####  End of days loop



if ($count_no_del &gt; 0){  #  If there are no delivery days specified
$errors[] = 'You must specify delivery hours for at least one day of the week.';
}

$days_delivery1 = implode(',', $days_del);
$days_delivery1 = $days_delivery1 . ' ; ';



foreach ($days_numbered as $num =&gt; $val){  #  Getting first day for which delivery hours have been specified
if (isset(${$val.'_times'}) &amp;&amp; (${$val.'_times'} != NULL)){
${'times_'.$num} = ${$val.'_times'};
$first_day = $num;
break;
}
}




$check_array = array_keys($full_list, ${'times_'.$first_day}));  #  checking how many other days match the delivery hours for the first specified day.

foreach ($check_array as $array_key){
$days_delivery2 .= $array_key;
}

$days_delivery2 .= " " . ${'times_'.$first_day};


$note_line = $days_delivery1 . " "  ; #  second part, something like 'MTH 9 To 5, TW 10 TO 5 CLOSED 1 TO 2, F 10 TO 2' should go a s the second part of the string.
?&gt;
</code></pre>

<p>As you can see, after getting the first part of the string in that format ($days_delivery1) I'm stuck and don't know what the hell am I doing. I have a vouge idea of using 2 different arrays (main one and numbered one) and using array_keys to find matching values, but any time I try to work on it I just run into a wall. Any ideas would be much apreciated.</p>
