Re: Probably very easy to do, but for some reason...



Paul F. Johnson wrote:

$ex = explode("," $v);
foreach ($ex as $e)
{
if ($e != "0")
$newtext .= " : " . $foo[$e];
}

The problem is that $e here is not being interpreted how I'd expect (I'd
assume it to be a number). Do I need to cast $e as an int or is there
something I'm missing?

$e will by of type string, since $ex is an array of strings, since it is the
result of an explode() of a string.
- either do an explicit typecast in case you are really sure, you will
always have numerical values in there (check it...)
- or rely on php to make an implicit typecast which _might_ work, but then
again which _might_ make problems and lead to unexpected results if you
have mixed values as keys in your array $foo.

arkascha
.



Relevant Pages

  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Trouble Using System.Array.ForEach
    ... The reason that the Array.ForEach Generic method is not suitable for your purposes in this case is that on each iteration of the 'hidden' loop, each element of the array is paseed to the Action delegate Sub, ByVal. ... Therefore the string that is passed to the delegate is returned unaltered and the parent array is not modified. ... And you may be right about the ForEach not fitting my conditions, I really can't say for sure since I have never had the opportunity to use ForEach, but either way, it never hurts to have extra knowledge so that if a situation were to come up where it was fit I would know it. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: foreach pretty useless for composite classes, dont ya thunk?
    ... Change the code to look like this in your foreach within main: ... I modified your original code to include an array of ints (in addition ... public string[] FirstNames; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need a simple database for name and email only
    ... function getMultipleSegments($segment, $xml) ... foreach ... $string = strtr; ... $supportedExtensions = array ( ...
    (alt.php)
  • Re: foreach statement output
    ... know about foreach loop workings. ... loop as though it was a C-style for loop. ... the number of characters in a string. ... elements in an array, we refer to the array's *size*. ...
    (comp.infosystems.www.authoring.cgi)