PHP: Problem with For Loops and If Statement

PharaohInc

New Member
Messages
8
Reaction score
0
Points
0
Ok,I'm having a problem. I have an array of arrays, the arrays inside the arrays are in that format shown below. What I want to do is take the shortest distance between the points but if they are in the same city the loop would discard them and move it to the next one that has the shortest distance to the point but not in the same city.

This is the keys of each of the normal arrays
$venue['city']
$venue['country']
$venue['xcoord']
$venue['ycoord']

The loops looks like it does below and there is a function that can calculate the distance between points already. The variable $startVenue has the same format as the normal arrays as well


PHP:
for($r=0;$r < 5; $r++)
{
$startVenue; // starting location or the first array
$venue1 = $startVenue;
$tour;
$venues; //contains the arrays with all the information of the venues
$shortestDistance = 999999999;
$index;


for($i=0;$i<sizeof($venues);$i++)
{
    $dist = distance($venue1,$venues[$i]);
    

    if(dist<$shortestDistance)
         {
      if($venue1['city']!=$venues[$i]['city'])
      {
         $shortestDistance = dist;
         $index = $i;
      }
         }

}
     array_push($tour,$venue1);  
     $venue1 = $venues[$index];
     unset($venues[$index]);
     array_values($venues);
}
The thing is if i take out the if statement with the city it works, giving points that are close but with the if statement for the city it gives me nothing, i have tried strcmp and it still does not work. I'm at wits end at what to do.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
PHP:
for($r=0;$r < 5; $r++)
Why 5?

PHP:
{
    $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.

PHP:
    $shortestDistance = 999999999;
    $index;

    for($i=0;$i<sizeof($venues);$i++) {
A foreach loop is easier and more readable here:

PHP:
    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.

PHP:
        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:
$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.
 
Last edited:

PharaohInc

New Member
Messages
8
Reaction score
0
Points
0
I tried your suggestion but I get an error in return now

Parse Error: Unexpected '{'

This normally occurs when you have mismatched brackets, i doubel checked with my code and i dont see where the problem is. I then took the function out and tested it by itself once again and I got that same error.
 

dlukin

New Member
Messages
427
Reaction score
25
Points
0
I tried your suggestion but I get an error in return now

Parse Error: Unexpected '{'

.

The error message usually includes the line number, which would help. Just work back from there and find the missing ( or ) or ;

Especially if you would post the new code. From the top of the script. Cutting a snippit out of the middle does not help. Trying to guess how the new code looks is futile.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
In addition to what dlukin says, that error message is a good example of why it's important to properly indent your code, preferably with an editor that auto-indents. It becomes very easy to see which braces match, and thus where you're missing a brace.
 
Top