# turning nested loops into a shorter algorithm

## Recommended Posts

all,

I have this block of code:

```        If (\$ctr = 4) {
for (\$block = 1; \$block <= 9; \$block++) {
for (\$ctr2 = 1; \$ctr2 <= 3; \$ctr2++) { //each line.
for (\$ctr3 = 1; \$ctr3 <= 3; \$ctr3++) { //char 1 - 3 in each line.
switch (\$ctr2) { //which line?
Case 1:
\$strOut = \$strOut . substr(\$Line1, \$ctr3, 1);
Case 2:
\$strOut = \$strOut . substr(\$Line2, \$ctr3, 1);
Case 3:
\$strOut = \$strOut . substr(\$Line3, \$ctr3, 1);
}
}
}
}
}
```

what I'd like to do is streamline it.  There are 3 lines of text being captured here.  What is (supposed to be) happening is that each block of characters (3 characters per line positions, starting at position 1 or positions every 3 char increments after that) is being captured until all 9 chars are stored in \$strOut.

9 chars = 3 chars on each line * 3 lines.

is there any better way to do this mathematically?  I realize that we can manipulate the variables and coding many different ways, but I'm not interested in that.  I'm only interested in using math or leveraging techniques to speed up the process here.  Resources from PHP would be OK but I'm trying to focus on the massive iteration and turning it into something more useful.

Edited by help_me_with_php
##### Share on other sites

Firstly, the first three character position are 0, 1 and 2 and not 1, 2 and 3.

Secondly, you don't need to call substr(), you can treat a string as an array of characters, so the the second char of \$line1 is \$line1[1].

So your for loops need run from 0 to 2 and the switch statement becomes

```switch (\$ctr2) { //which line?

Case 1:
\$strOut .= \$Line1[\$ctr3];
break;
Case 2:
\$strOut .= \$Line2[\$ctr3];
break;
Case 3:
\$strOut .= \$Line3[\$ctr3];
break;
}
```
##### Share on other sites

Hello Barand,

yeah I realize what you said.  I was going off base 1 instead of base 0 for functions.  no biggie.

your code though is not really what I was looking for.  the syntax of yours is just shorter than mine.  nothing really changed.  thanks for responding.

##### Share on other sites

It may be helpful to see some samples. Could you show an example of what the three lines of text might look like and what the resulting \$strOut would be?

##### Share on other sites

sure.

I know the code isn't perfect. It's not supposed to be. what it is *supposed* to do is take 9 blocks of 3-chars-each from each of the 3 lines, one block at a time.

I'm not going to change the code because I know it's wrong just a tad, but you get the idea. here is a sample of the 3 lines of text I have:

1.2.3.4.5.6.7.8.9.10.11.12.
1+2+3+4+5+6+7+8+9+10+11+12+
1-2-3-4-5-6-7-8-9-10-11-12-

so the first iteration of the loop here would produce the following in "\$strOut"::

1.21+21-3

first 3 first line + first 3 second line + first 3 third line.

it reality, the code should do this for all 9 blocks of 3 characters (there are 27 char positions on each line).

thanks!

##### Share on other sites

I'm probably missing something, but should the first iteration result in the following:

1.21+21-2

If that's the case, would the second iteration result with:

.3.+3+-3-

##### Share on other sites

I'm probably missing something, but should the first iteration result in the following:

1.21+21-2

YES, that's my mistake.  thanks.

If that's the case, would the second iteration result with:

.3.+3+-3-

YES.  that's the second block of 3.

##### Share on other sites

Could you just break the strings using something like str_split() and then just merge the arrays?

```<?php

<?php
\$line1 = '1.2.3.4.5.6.7.8.9.10.11.12.';
\$line2 = '1+2+3+4+5+6+7+8+9+10+11+12+';
\$line3 = '1-2-3-4-5-6-7-8-9-10-11-12-';

\$split1 = str_split(\$line1, 3);
\$split2 = str_split(\$line2, 3);
\$split3 = str_split(\$line3, 3);

\$strOut = '';
\$count  = count(\$split1);
for(\$i=0; \$i<\$count; \$i++) {
\$strOut .= '<div>' . \$split1[\$i] . \$split2[\$i] . \$split3[\$i] . '</div>';
}

print \$strOut;
?>```
##### Share on other sites

yes, that's awesome.  yes I could.

but again, I'm really interested in a mathematical algorithm of somekind that you can map to this whole thing.  there are an abundance of PHP internal resources and functions that you can use to help out in this case.  but what I'm trying to show here is algorithmic coding, not internal function use.

help me out on that too, if you will.  is what I'm saying making any sense at all?

##### Share on other sites

try

```\$lines = array (
'1.2.3.4.5.6.7.8.9.10.11.12.',
'1+2+3+4+5+6+7+8+9+10+11+12+',
'1-2-3-4-5-6-7-8-9-10-11-12-'
);

\$output = array();

for (\$pos = 0, \$k = strlen(\$lines[0]); \$pos < \$k; \$pos+=3) {
\$output[\$pos/3] = '';
foreach (\$lines as \$line) {
for (\$c = 0; \$c<3; \$c++) {
\$output[\$pos/3] .= \$line[\$pos+\$c];
}
}
}

// show results
echo '<pre>',print_r(\$output, true),'</pre>';```
##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.