The code you posted is all kinds of wrong, so I'm guessing it's heavily edited from your real code. It should also be properly indented to make it readable.

Originally Posted by
PharaohInc
PHP Code:
for($r=0;$r < 5; $r++)
Why 5?

Originally Posted by
PharaohInc
PHP Code:
{
$startVenue; // starting location or the first array
$venue1 = $startVenue;
$tour;
$venues; //contains the arrays with all the information of the venues
Were these just to show us some of the variables you're using? If not, they all hold null values. The $venue1 = $startVenue; should be outside the loop.

Originally Posted by
PharaohInc
PHP Code:
$shortestDistance = 999999999;
$index;
for($i=0;$i<sizeof($venues);$i++) {
A foreach loop is easier and more readable here:
PHP Code:
foreach ($venues as $i => $venue) {
$dist = distance($venue1,$venue);
...
It's also more correct, since you're removing items from $venues but not reindexing. With a for loop, you'll need to check that an item exists for a given index.

Originally Posted by
PharaohInc
PHP Code:
if(dist<$shortestDistance)
{
if($venue1['city']!=$venues[$i]['city'])
{
$shortestDistance = dist;
dist here is a bareword, which will be converted to a string, unless there's a constant with the same name. Either is wrong; you want $dist to get a variable.
Here's a cleaned up version of your sample code:
PHP Code:
$venues = array(
array('city' => ...),
array('city' => ...),
...
);
$fromVenue = $startingVenue;
$tour = array();
// this will repeat while $venues has at least one element
while ($venues) {
$minDistance = PHP_MAX_INT;
foreach ($venues as $i => $venue) {
$dist = dist($fromVenue, $venue);
if ($dist < $minDistance
&& $fromVenue['city'] != $venue['city'])
{
$minDistance = $dist;
$index = $i;
}
}
// when pushing a single element, empty index syntax is preferred over array_push
$tour[] = $fromVenue = $venues[$index];
unset($venues[$index]);
}
Are you trying to find the shortest path that visits the venues? This is basically the traveling salesman problem (except that for the TSP you need to find a circuit rather than a path, which doesn't affect the complexity), and the only known way of solving it in general is to generate and check every possible path.