Jump to content

PLEASE HELP clear ARRAY


jonniejoejonson

Recommended Posts


Okay I have a fairly long and complicated (becuase I am new to scripting) piece of code. it is very close to working
but it is giving me one strange reading. the final unset($aryHundreds); is not deleteing the array how I want. However
if you test the array after the unset, it has deleted it. However I think it is a timming issue. As it seems to be giving
me the results of all three lots of results.


1/
function A($origZip,$origLng,$origLat){
//This runs function starter roughly 3 times.
starter($origZip,$origLng,$origLat);}

2/
function starter($origZip,$origLng,$origLat){
//This then runs function distance 1000 times, for each of the 3 times that starter has been run.
distance($origZip1,$lon1,$lat1,$zip2,$lon2,$lat2);}


function distance($origZip1,$lon1,$lat1,$zip2,$lon2,$lat2) {
$distance = (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);
print "<br>$origZip1.$distance";
global $aryHundreds;
global $counter;
global $numberOfStatesToCompare;
$counter++;

if($distance<100){
print $zip2."under 100";
array_push($aryHundreds, $zip2);}

if($numberOfStatesToCompare==$counter){
$counter=0;
$stringHundred='';
for ($i=0;$i<count($aryHundreds);$i++) {
$stringHundred.=$aryHundreds[$i].'|';}
print 'markerHere:'.count($aryHundreds).'string'.$stringHundred;
unset($aryHundreds);
}


//like i mentioned the only problem is that unset($aryHundreds) does not seem to be in the right place, as it needs to be
//deleted before the second lot of function A is executed. Oddly all the results are correct, but just being repeated three times over by
//the time the third lot of funcitn A is exectued.

Thanks top any responders. jonnie
Link to comment
https://forums.phpfreaks.com/topic/27396-please-help-clear-array/
Share on other sites

Thanks for the reply but it doesn't work either. It is strange becuase if you try and use the array after the unset within the function it has been deleted. However I assume that the other functions have executed the final function before it clears the array, however the output would suggest otehrwise. Anyone else any further ideas?
1) Without indenting, your code is very hard to follow so I copy/pasted it into my editor and did some indenting so that I could follow it.

2) I don't understand how this is working--you're missing a curly brace (it's marked).

3) Try putting the unset AFTER the if statement (it's marked).  See what it does.  I'm not sure.

<?PHP
function A($origZip,$origLng,$origLat){
  //This runs function starter roughly 3 times.
  starter($origZip,$origLng,$origLat);
}
function starter($origZip,$origLng,$origLat){
  //This then runs function distance 1000 times, for each of the 3 times that starter has been run.
  distance($origZip1,$lon1,$lat1,$zip2,$lon2,$lat2);
}
function distance($origZip1,$lon1,$lat1,$zip2,$lon2,$lat2) {
  $distance = (3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180);
  print "
  $origZip1.$distance";
  global $aryHundreds;
  global $counter;
  global $numberOfStatesToCompare;
  $counter++;
  if($distance<100){
      print $zip2."under 100";
      array_push($aryHundreds, $zip2);
  }

  if($numberOfStatesToCompare==$counter){
      $counter=0;
      $stringHundred='';
      for ($i=0;$i<count($aryHundreds);$i++) {
          $stringHundred.=$aryHundreds[$i].'|';
      }
      print 'markerHere:'.count($aryHundreds).'string'.$stringHundred;
      //unset($aryHundreds);//  <--moving this to outside the if statement
  }
  unset($aryHundreds);//        <--moved from above
}
?>
Thanks both for your responses. I have manipulated my script somewhat, and it still does not work using unset, however by resetting it using $aryHundreds = array();now works with my changed code. So that is brilliant. What isn't so good is that it still isn't as fast as I hoped!!!. However it will have to do. Thanks both for your help. Jonnie.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.