Jump to content

foreach problem


NaGGi

Recommended Posts

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  :confused:

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.