# Logic For A Peculiarly Ordered Array

## Recommended Posts

Hey everyone, I am having trouble with some PHP logic concerning an array. This is what I need to do:Print the 3rd array valuePrint the 2nd array valuePrint the 1st array valuePrint the 6th array valuePrint the 5th array valuePrint the 4th array valueetc...But I also need to ignore the empty values if I have a number not divisible by 3 (i.e. an array of 7 values, don't print for 9 and 8).This is probably pretty easy for someone with a lot of programming experience, so if you are bored and would like to help me out I would really appreciate it. Thanks,Justin

Edited by jwburnside
##### Share on other sites

There's probably a prettier way to do this that wouldn't require 3 separate statements in the for-loop. This would be ugly indeed if we were grouping by nines! But since it is only three, I figured I'd just brute force it.EDIT: prettier version in next post.The only trick is to remember you can increment your loop counter by threes. The rest is routine.I assume you can adapt this? You've been around long enough.

`<?php	function byThrees(\$a) {		\$len = count(\$a) + 2;		\$str = "";		for (\$i = 2; \$i < \$len; \$i += 3) {			\$str .= isset(\$a[\$i]) ? \$a[\$i] . '<br>' : "";			\$str .= isset(\$a[\$i - 1]) ? \$a[\$i - 1] . '<br>' : "";			\$str .= \$a[\$i - 2] . '<br> <br>';		}		return \$str;	}	\$arr = array (		'a',		'b',		'c',		'd',		'e',		'f',		'g',		'h',		'i',		'j',		'k',		'l',		'm'	);	echo byThrees(\$arr);?>`

And for goodness' sake, don't anyone start thinking I like <br> elements and non-breaking spaces! This is a demo.

##### Share on other sites

I had a minute while the pizza's coming. This version is a little more robust.

`function reverseGroupByIncrement(\$a, \$inc) {	\$len = count(\$a) + \$inc - 1;	\$str = "";	for (\$i = \$inc - 1; \$i < \$len; \$i += \$inc) {		for (\$j = 0; \$j < \$inc; ++\$j) {			\$str .= isset(\$a[\$i - \$j]) ? \$a[\$i - \$j] . '<br>' : "";		}		\$str .= ' <br>';	}	\$str = substr(\$str, 0, -14); // remove extra chars	return \$str;}\$increment = 3; // change me to suitecho reverseGroupByIncrement(\$arr, \$increment);`

##### Share on other sites

I had a minute while the pizza's coming. This version is a little more robust.
`function reverseGroupByIncrement(\$a, \$inc) {	\$len = count(\$a) + \$inc - 1;	\$str = "";	for (\$i = \$inc - 1; \$i < \$len; \$i += \$inc) {		for (\$j = 0; \$j < \$inc; ++\$j) {			\$str .= isset(\$a[\$i - \$j]) ? \$a[\$i - \$j] . '<br>' : "";		}		\$str .= ' <br>';	}	\$str = substr(\$str, 0, -14); // remove extra chars	return \$str;}\$increment = 3; // change me to suitecho reverseGroupByIncrement(\$arr, \$increment);`

I wish I had noticed your second post, would have saved me some time! But here is what I came up with that seems to work ok:
`//Created Array called \$postarray//Determine how many groups of 3 there will be and add 1\$groupcount = (count(\$postarray) / 3) + 1;if (is_float(\$groupcount))	\$groupcount = substr(\$groupcount, 0, stripos(\$groupcount, ".") ); //strip decimal if need befor (\$j = 1; \$j <= \$groupcount; \$j++) { //iterate however many times there are groups	\$post = \$j * 3;  //this gives you 3,6,9 etc...	for (\$i = 1; \$i <=3; \$i++) { //display the 3 posts in descending order.		if (isset(\$postarray[\$post]["title"]))		  echo \$postarray[\$post]["title"] . "<br />";		\$post--;	}}`

Might not be pretty, but it will do. Thanks for your help,JW