?
Solved

how to cast a variable in a foreach loop?

Posted on 2008-10-01
4
Medium Priority
?
1,393 Views
Last Modified: 2012-05-05
Hi,
i'm trying to loop through a list of float values and compare them.
turns out i can't use regular type casting in a foreach loop.

i looked around but i couldn't find any way i can actually cast there.
maybe there's a trick?
any help is greatly appreciated.


here's my code:


$zDate = 1.19; // debug.
		$zodiacs = array (
				0.00 => "Sagittarius",
				1.20 => "Capricorn",
				2.19 => "Auarius",
				3.21 => "Pisces",
				4.20 => "Aries",
				5.21 => "Taurus",
				6.21 => "Gemini",
				7.23 => "Cancer",
				8.23 => "Leo",
				9.23 => "Virgo",
			   10.24 => "Libra",
			   11.22 => "Scorpio",
			   12.22 => "Sagittarius",
		);
		
		foreach ($zodiacs as $key =>$value)
//		foreach ($zodiacs as (float)$key =>$value) was my first reaction, but that does not work.
		{
			$fkey = (float)$key;   //even this doesn't work
			if($zDate > $fkey)
			{
//$fkey will always be a rounded value here. e.g. 1, 2, or 3, ect...
				$zodiacName = $value;
				continue;
			}
			
		}

Open in new window

0
Comment
Question by:nOOgz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 22617596
please try this:
    $zodiacs = array (
                        "0.00" => "Sagittarius",
                        "1.20" => "Capricorn",
                        "2.19" => "Auarius",
                        "3.21" => "Pisces",
                        "4.20" => "Aries",
                        "5.21" => "Taurus",
                        "6.21" => "Gemini",
                        "7.23" => "Cancer",
                        "8.23" => "Leo",
                        "9.23" => "Virgo",
                     "10.24" => "Libra",
                     "11.22" => "Scorpio",
                     "12.22" => "Sagittarius",
            );

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22617614
you need "string" keys, not floats - then do the cast:
$zDate = 1.19; // debug.
            $zodiacs = array (
                        "0.00" => "Sagittarius",
                        "1.20" => "Capricorn",
                        "2.19" => "Auarius",
                        "3.21" => "Pisces",
                        "4.20" => "Aries",
                        "5.21" => "Taurus",
                        "6.21" => "Gemini",
                        "7.23" => "Cancer",
                        "8.23" => "Leo",
                        "9.23" => "Virgo",
                     "10.24" => "Libra",
                     "11.22" => "Scorpio",
                     "12.22" => "Sagittarius"
            );

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22617618
the problem is that any numerical value is considered a array index, and not a array key information, hence implicitly casted as int
0
 
LVL 1

Author Closing Comment

by:nOOgz
ID: 31502109
totally. duh .. thank you so much :)
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question