freelance84 Posted September 16, 2011 Share Posted September 16, 2011 $results = array('11','11','11'); $counter = 0; foreach($results as $baseKey => $baseValue) { print_r($results); //loop through all the $results counting the occurances of $baseValue foreach($results as $key => $valueToFind) { if($valueToFind == $baseValue) { unset($results[$key]); ++$counter; } } } results in: "Array ( [ 0 ] => 11 [ 1 ] => 11 ) Array ( )Array ( )" Is this because the foreach function analyses the array and sets the number of times it should run the loop at the beginning and never analyses the array again? The only way i could stop the loop from running if all values were unset was to add this at the end of the loop: if(empty($results0to4)) { break; } Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/ Share on other sites More sharing options...
Pikachu2000 Posted September 16, 2011 Share Posted September 16, 2011 It looks like you're trying to unset an array element based on whether its value is present in another array, correct? Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270030 Share on other sites More sharing options...
ManiacDan Posted September 16, 2011 Share Posted September 16, 2011 Is this because the foreach() function analyses the array and sets the number of times it should run the loop at the beginning and never analyses the array again? Yes, the array the loop operates on is not the same array as what's available inside the loop. I wrote an article on this subject a few years ago. You're basically going to empty this array regardless of its contents. What are you trying to do with this loop? From what I can see, no matter what the initial settings of the array are, it will be empty when you're done. Seems like a lost of wasted cycles to do that task. Also, the output you claim you're getting isn't correct, there should be 3 items in the first array. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270032 Share on other sites More sharing options...
freelance84 Posted September 16, 2011 Author Share Posted September 16, 2011 yea sorry that was a typo. There is three outputted from this example. Yea, it is rather complex to explain fully, but essentailly there is three sets of results coming from different sources: $resultsGr0 $resultsGr1 $resultsGr2 Some of the results are duplicated and spread across the different arrays. EG: $resultsGr0 = array('11','11','123'); $resultsGr1 = array('234','11','75'); $resultsGr2 = array('123','18','66'); In the above example, the result '11' appears twice in $resultsGr0 and once in $resultsGr1. The script i have written loops through the first array counting the number of occurrences of the foreach value in its own array. It then counts how many times the value appears in each of the other arrays, and every time it finds a match, it removes the said value so as not to count it again later. After reaching the end it then decides where to place the 'value'. There is then a subsequent script which does a similar looping process through the latter two arrays (as the first array is now empty), and it decides where to place the values accordingly. Then finally there is a foreach loop on the last array (although it may now be empty also) The arrays above in reality are actually not as simple as i have made out, this was just to illustrate my question. However, everything works like a charm when I add the break; into the end of each of the foreach loops. I was just wondering how the foreach function actually worked. I will have a read of your article ManiacDan, thanks Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270037 Share on other sites More sharing options...
freelance84 Posted September 17, 2011 Author Share Posted September 17, 2011 (after reading the article from ManiacDan) If the foreach array places a copy into the servers memory at the start and then loops through accordingly, if the array is big then is foreach the wrong tool to use? foreach is very hand for being lazy, but if it dumps excess info into memory would it be better to be using a for loop and just use the value of $i as the key? For example, which would be more efficient: $words = array( a 4000 part array ); $count = count($words); //loop1 foreach($words as $key = > $value) { echo $key.' - '.$value.' <br/>'; } //loop2 for($i = 0 ; $i < $count ; ++$i) { echo $i.' - '.$words[$i].'<br/>'; } //loop3 (i'm guessing this will be less efficient than loop2 as it has to keep counting the array each time for($i = 0 ; $i < count($words) ; ++$i) { echo $i.' - '.$words[$i].'<br/>'; } Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270221 Share on other sites More sharing options...
jcbones Posted September 17, 2011 Share Posted September 17, 2011 <?php $arr = range(0,4000); $count = count($arr); //loop1 $start = microtime(true); foreach($arr as $key => $value) { $key . $value; } $end = microtime(true); echo 'foreach: ' . number_format(($end - $start),5) . ' seconds!<br />'; //loop2 $start = microtime(true); for($i = 0 ; $i < $count ; ++$i) { $arr[$i]; } $end = microtime(true); echo 'for: ' . number_format(($end - $start),5) . ' seconds!<br />'; ?> results foreach: 0.00312 seconds! for: 0.00065 seconds! Indeed, for is faster! Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270258 Share on other sites More sharing options...
freelance84 Posted September 17, 2011 Author Share Posted September 17, 2011 WOW, by a considerable amount too! OK, from now on, unless it is really essential i think i shall be avoiding the foreach function. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270278 Share on other sites More sharing options...
ManiacDan Posted September 19, 2011 Share Posted September 19, 2011 Told you ;-) Foreach is appropriate if you don't know the keys in advance, though you could still do: $keys = array_keys($aHugeArray); for ( $p = 0; $p < count($keys); $p++ ) { $theVal = $ahugeArray[$keys[$p]]; } As for your actual problem, if the example arrays aren't too complex you might be able to solve this entirely using array_merge and array_unique. Maybe. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/247295-foreach-only-analyses-array-at-the-start/#findComment-1270657 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.