Problem iterating through array numbers above and below a certain value

Hi,

I have an array with 4 values I am running throgh in a foreach loop. Each iteration of this has another array with the same values which also runs in a loop.

The code I am having trouble writing is this:

  • run through array
  • run through second array in nested loop
  • check if nested array value is == to parent array value
  • if nested array value is < parent, what is the difference. Get diff and multiply by -520
  • if nested array value is == parent, keep same value
  • if nested array value is > parent, what is the differnce in values. get diff and multiply by 520

here is some code I have…can’t solve it at the moment though. hopefully someone can help:

<?php
$panels = 4;
for($arrnum = 1;$arrnum <= $panels;$arrnum++){
	$panelarray[$arrnum] = $arrnum;
}
//print_r($panelarray);
foreach($panelarray as $tabcount){
	if($tabcount != 1){//if not 1st tab, run it

		$tabs = $panels;
		$prev = $tabs - ($tabcount+1);
		$next = $tabs - ($prev + 1);
		
		for($innertab = 1;$innertab <= $panels;$innertab++){
			
			if($innertab < $tabcount){
				$denom = $prev;
				$buttonslide = $denom * (-520);
			}
			if($innertab > $tabcount){
				$buttonslide = $next * 520;
				$next = $next + 1;
			}
			
			if($innertab == $tabcount){
				echo $tabcount;
			}
	
		}//end for
	}//end if not tab 1
}//end foreach
?>

Thanks

what are the names of your 2 arrays?

I see only 1 array.

Sorry, my bad. Got mixed up posting this.

It is 1 array then a for loop that matches the number of values in the array…the variable $panels.

it could be done with 2 foreach loops as well. I’ve just been trying different ways to get it working and think I’ve been stairing at it too long!

Why would you want to match all values with all values? Shouldn’t it be
one value of the 2nd array with one from the first? Your code right will do 4
actions for 1 number 4 times.

Yeah, I need it to go through each number and then run another loop inside this for each number.

If the number of values is 5, run 5 times and within each iteration, run a sub-loop 5 times that checks the current value of the sub-loop against the value of the parent loop. If the sub-loop value is less than the parent loops, multiply the sub-loops position * -520. If the sub-loop value is greater than the parent loop, multiply it by the position away from the parent loop value * 520. ie:

  • code runs 1st and parent loop is at [1] on first iteration

  • subloop runs

  • second loop is at [1][1] so value [1] (parent) == [1] (subloop)

  • second loop is at 12 so value is 1 place above the parent loop…so 1 * 520

  • second loop is at [1][3] so value is now 2 places above the parent loop…so 2 * 520

  • second loop is at [1][4] so value is now 3 places above the parent loop, so 3 * 520

  • sub loop has run 4 times so finished and flow moves back to parent loop, which now iterates again which leads to;

  • code runs 2nd time and parent loop is at [2] on second iteration

  • subloop runs

  • second loop is at [2][1] so value [2] (parent) < [1] (subloop). The distance of the subloop position away from the parent loop position is 1. As it is subloop position < parent we mutliply * -520

Hope I have explained this well enough! It may seem like a strange setup but this is the logic I need for my application which will be a flat HTML code generator for something I am making that cant use PHP or anything like that.

Anyone able to help?