NaGGi Posted November 24, 2009 Share Posted November 24, 2009 I'm quite noobie with PHP and I'm trying to achieve something with foreach and I dont really understand why I get the result I get My original code is this: foreach( $rate_details as $k => $v ) { if( $k == 3 ) { echo $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { echo $v.'; '; } } $rate_details array includes 5 values and I'd like to change the output when there is a certain value. Is this possible? Tried to do it like this when $k value is 'Matkahuolto' but this doesnt work: foreach( $rate_details as $k => $v ) { if ($k == 2 && $v == Matkahuolto && $vendor_freeshipping <= round($total, 2)) { echo 'Rahti vapaa'; } else { if( $k == 3 ) { echo $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { echo $v.'; '; } } } So I'd like to see only that 'Rahti vapaa' when there is certain values but it still prints everything else too. Guess I dont understand the behavior of foreach properly Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/ Share on other sites More sharing options...
Yesideez Posted November 24, 2009 Share Posted November 24, 2009 Can you post the output of this please? var_dump($rate_details); Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964642 Share on other sites More sharing options...
NaGGi Posted November 24, 2009 Author Share Posted November 24, 2009 var_dump($rate_details); -> array(5) { [0]=> string(17) "standard_shipping" [1]=> string(11) "Matkahuolto" [2]=> string(11) "Matkahuolto" [3]=> string(4) "5.90" [4]=> string(2) "22" } Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964647 Share on other sites More sharing options...
Yesideez Posted November 24, 2009 Share Posted November 24, 2009 Sorry - can you edit that and place between CODE tags? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964650 Share on other sites More sharing options...
NaGGi Posted November 24, 2009 Author Share Posted November 24, 2009 Dont find any edit button so I write it again. array(5) { [0]=> string(17) "standard_shipping" [1]=> string(11) "Matkahuolto" [2]=> string(11) "Matkahuolto" [3]=> string(4) "5.90" [4]=> string(2) "22" } Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964658 Share on other sites More sharing options...
Anzeo Posted November 24, 2009 Share Posted November 24, 2009 What is de output of the foreach now? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964681 Share on other sites More sharing options...
NaGGi Posted November 24, 2009 Author Share Posted November 24, 2009 It gives: Matkahuolto; Matkahuolto; 5.90€; For some reason it doesnt give out what it did previously. Maybe some cache thing. Dunno but it gave out: Matkahuolto; Rahti vapaa; 5.90€; But I want it to give out just 'Rahti vapaa' when foreach founds out 'Matkahuolto' from that $rate_details array. If there is something else than 'Matkahuolto' in the array it would normally give out what it does now. Is this possible with one foreach? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964704 Share on other sites More sharing options...
Anzeo Posted November 24, 2009 Share Posted November 24, 2009 try foreach( $rate_details as $k => $v ) { if ($k == 2 && $v == "Matkahuolto" && $vendor_freeshipping <= round($total, 2)) { echo 'Rahti vapaa'; } else { if( $k == 3 ) { echo $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { echo $v.'; '; } } } I think the problem was that you were comparing against Matkahuolto which is a constant, you need to surround it with quotes. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964713 Share on other sites More sharing options...
NaGGi Posted November 24, 2009 Author Share Posted November 24, 2009 Aint that exactly the same code I have been trying? Well didnt do anything. Guess that 'if ($k == 2...' doesnt work now. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964723 Share on other sites More sharing options...
MatthewJ Posted November 24, 2009 Share Posted November 24, 2009 Sorry - can you edit that and place between CODE tags? For a var dump with 5 items in it and no code whatsoever? picky picky Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964727 Share on other sites More sharing options...
Anzeo Posted November 24, 2009 Share Posted November 24, 2009 Aint that exactly the same code I have been trying? Well didnt do anything. Guess that 'if ($k == 2...' doesnt work now. No, notice the double quotes around Matkahuolto. Also are you sure the third condition is true? ($vendor_freeshipping <= round($total, 2)) @MatthewJ: What's the use of commenting, when it's off-topic? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-964732 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 Ok new day and new strenght Yeah I did forget something when I recreated the problem global $total Before that if sentence and that if sentence didnt work at all. Now I get the results which I find weird (double quotes or not) which is: Matkahuolto; Rahti vapaa5.90€; So that if sentece is true but it still mixes it with the else statement too. Normally: Matkahuolto; Matkahuolto;5.90€; after adding if statement Matkahuolto; Rahti vapaa5.90€; I want it to give only 'Rahti vapaa' when that if statement is true. Why its not working? The $rate_details array is this: 0 = standard shipping 1 = Matkahuolto 2 = Matkahuolto 3 = 5.90 4 = 22 Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965291 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 Well you have echo's in your else statement, if you would remove them only 'Rahti vapaa' will be printed out. The foreach works but you echo both in the if and in the else statement, that's why it always prints. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965338 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 Yes I can see that it echoes out both if and else part but why it does that? How that should be coded to to get out only 'Rahti vapaa' when there is 'Matkahuolto' in the array? It should echo the else part if there is something else than 'Matkahuolto' in the array. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965345 Share on other sites More sharing options...
Yesideez Posted November 25, 2009 Share Posted November 25, 2009 Sorry - can you edit that and place between CODE tags? For a var dump with 5 items in it and no code whatsoever? picky picky The reason was because I was hoping the CODE tags would reserve the carriage returns as the dump looked a little malformed (first element) but wasn't sure. I always read my var_dumps by viewing the page source - it all stands out a lot easier. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965351 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 Yes I can see that it echoes out both if and else part but why it does that? How that should be coded to to get out only 'Rahti vapaa' when there is 'Matkahuolto' in the array? It should echo the else part if there is something else than 'Matkahuolto' in the array. Well... it does do that? Or do you mean: It should only print 'Rahti vapaa' when the array contains 'Matkahuolto'? Then you should rewrite it to something like this: foreach( $rate_details as $k => $v ) { if ($k == 2 && $v == "Matkahuolto" && $vendor_freeshipping <= round($total, 2)) { echo 'Rahti vapaa'; break; } } Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965359 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 Yes, I want it to print only 'Rahti vapaa' and nothing else if there is 'Matkahuolto' in the array but it should print it normally if there is something else than 'Matkahuolto'. But I start to see now why it doesnt do that and dunno if that can be done inside foreach and that is really my question. Can it be done inside foreach? One more clarification what I want to see: Case 1 $rate_details array = 0 = standard shipping 1 = Matkahuolto 2 = Matkahuolto 3 = 5.90 4 = 22 Case 1 Result: Rahti Vapaa Case 2 $rate_details array = 0 = standard shipping 1 = DPS 2 = DPS 3 = 5.90 4 = 22 Case 2 Result: DPS; DPS; 5.90€ Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965373 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 I think this should work (or at least is the direction you shouldb e heading: foreach( $rate_details as $k => $v ) { if ($k == 2 && $v == Matkahuolto && $vendor_freeshipping <= round($total, 2)) { echo 'Rahti vapaa'; [b]break;[/b] } else { if( $k == 3 ) { echo $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { echo $v.'; '; } } } Notice I added the break, this will force the code out of the foreach. This ofcourse will only work if your first element is not printed out (which, if I look at your code, is not the case). I hope this is the solution Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965378 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 That break doesnt still solve the problem and you already said why. It sill prints that element 1 from the array. It breaks when it gets to that element 2 leaving the 3th out though problem seems to be that it still reads this part for element 0 and 1 even if there is 'Matkahuolto' elseif( $k > 0 && $k < 4) { echo $v.'; '; } 0 is left out leaving element 1 and 2 then break -> Matkahuolto;Rahti vapaa Maybe this is just not possible to do like I'm trying? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965387 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 Ok sorry misunderstanding , this should work: if(in_array("Matkahuolto",$rate_details) { echo 'Rahti vapaa'; }else{ foreach( $rate_details as $k => $v ) { if( $k == 3 ) { echo $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { echo $v.'; '; } } } Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965390 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 That definately works but now its not inside foreach anymore Main question is: Can it be done inside foreach? Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965394 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 That definately works but now its not inside foreach anymore Main question is: Can it be done inside foreach? Why is the above not OK? Otherwise: $output = ""; foreach( $rate_details as $k => $v ) { if ($k == 2 && $v == Matkahuolto && $vendor_freeshipping <= round($total, 2)) { $output = 'Rahti vapaa'; [b]break;[/b] } else { if( $k == 3 ) { $output .= $CURRENCY_DISPLAY->getFullValue( $v )."; "; } elseif( $k > 0 && $k < 4) { $output .= $v.'; '; } } } echo $output; Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965404 Share on other sites More sharing options...
NaGGi Posted November 25, 2009 Author Share Posted November 25, 2009 Your code was OK if I would be interested in the results only but I was trying to learn something here and your latest code is actually first ok answer for my question Interesting way to do it. Now I just have to understand why it works heh. Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965412 Share on other sites More sharing options...
Anzeo Posted November 25, 2009 Share Posted November 25, 2009 Your code was OK if I would be interested in the results only but I was trying to learn something here and your latest code is actually first ok answer for my question Interesting way to do it. Now I just have to understand why it works heh. Ok, if you need help with it gimme a call Link to comment https://forums.phpfreaks.com/topic/182767-foreach-problem/#findComment-965413 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.