Solved

how to cast a variable in a foreach loop?

Posted on 2008-10-01
4
1,379 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
  • 2
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
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
Comment Utility
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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
totally. duh .. thank you so much :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now