[SOLVED] Nested for loop while constructing multidimensional array

Hi All

I am trying to nest a for loop inside a multidimensional while it is being constructed. The new array is being built from other arrays. I need the data to exist as a block of data while I perform some actions on it (re-sort based on time). An array seemed the logical way to do it.

Here is my code. It is failing with the following error - syntax error, unexpected ‘for’ (T_FOR), expecting ‘)’. The identified line is the one starting with for.

$RaceResults = array(
                               for ($x=0; $x<=6; $x++) { 
                                            array($x, $LaneAssignment[$x], $RaceResultsArray[$x]),
                                            }
                                        );
                                        
                                print_r $RaceResults;

All help appreciated

Joe

Quick response while working, sorry if it’s inaccurate. You can’t do a loop inside of an actual array declaration. You can however create the array as an empty one, and then add individual items (in this case, more arrays) as array items. It could look (something) like this? Without knowing where this data comes from, what LaneAssignment and RaceResultsArray are, it’s hard to know what you need?

$RaceResults = array();
for ($x=0; $x<=6; $x++) { 
    $result = array($x, $LaneAssignment[$x], $RaceResultsArray[$x]);
    $RaceResults[] = $result;
}

I don’t know how you have your array setup. but why don’t you simply do a uasort on the array? Or usort depending on what kind of array you have?

For example using uasort:

    <?PHP
$raceResults = array(
   'Big Red' => array('time' => 183, 'lane_assignment' => 'Gate  1', 'position' => 4),
	 'Shotgun' => array('time' => 167, 'lane_assignment' => 'Gate 5', 'position' => 2),
	 'Happy Meal' => array('time' => 178, 'lane_assignment' => 'Gate 3', 'position' => 3),
	 'Lady Lisa' => array('time' => 166, 'lane_assignment' => 'Gate 7', 'position' => 1) 	 
);

// Sort by time 
function time_sort($x, $y) {
	return ($x['time'] > $y['time']);
}

echo '<h2>Array Sorted as is</h2><pre>' . print_r($raceResults, 1) . '</pre>';
uasort($raceResults, 'time_sort');
echo '<h2>Array Sorted by Time</h2><pre>' . print_r($raceResults, 1) . '</pre>';

What’s the structure of the arrays the new array will be grabbing data from?

gidday: I dont know why you want to store $x ?
jeffrey: the $result variable is actually redundant here, just do a straight assignment of $RaceResults.
Pepster: I’m going to assume that the value coming out of $RaceResultsArray is the time; as $x is an integer 0-6 and lane assignment seems to be irrelevant data as well; also, his example shows he is not using associative arrays (though it may be beneficial for him to do so);

Based on his example, here’s how I would do it;

$RaceResults = array();
foreach($LaneAssignment AS $index => $lane) { 
  $RaceResults[] = array($lane, $RaceResultsArray[$index])
}
usort($RaceResults, function($a,$b) { return $a[1] > $b[1]; })

Thanks Folks. I used jeffreylees example. My background is vbscript and I’m making a very painful journey to PHP.