ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

[PHP] Variables in array to int ?

(1/1)

RedPillow:
I got this IF - FOREACH - THING here (I know that's a bit stupidly done ... adding "," to $num and removing it ...
My excuse is, that Im just a poor student with goal to someday become good at this kind of stuff and currently I don't care if there's something done stupidly ... cause if I ever try to fix that, it will not work):


--- Code: PHP ---if (isset($_GET["num"])) { foreach($_GET["num"] as $arvo) $num = $num . $arvo . ",";$numchk[] = $num;}$numchktostring = $numchk[0];$num2 = explode(",", $numchktostring); unset($num2[7]);$validchk = count($num2);
Now, it somewhy gives me output like this (By var_dumping $num2):

--- Code: PHP ---array(7) { [0]=>  string(1) "1" [1]=>  string(1) "2" [2]=>  string(1) "3" [3]=>  string(1) "4" [4]=>  string(1) "5" [5]=>  string(1) "6" [6]=>  string(1) "7" }
As you can see, every single key is a string.
My guestion is plain simple; how to make those strings int?

I have this example-snippet here, but how do I use it to array? (loop preferred):

--- Code: PHP ---$str = "10";      $num = (int)$str;
-- EDIT --

This seems to turn the variables to int, but it loses the avlue given to them before:


--- Code: PHP ---$i = 0;$a = 0;While ($i < 7) {$num2[$a] = (int)$num2[$a];$i++;$a++;}
var_dump($num2) outputs now:

--- Code: PHP ---array(7) { [0]=>  int(1) [1]=>  int(2) [2]=>  int(3) [3]=>  int(4) [4]=>  int(5) [5]=>  int(6) [6]=>  int(7) }

kyrathaba:
Rather than the following...


--- ---$num = (int)$str;

...you need something like this...


--- ---$num = intval($str);

The former looks more like casting in C#.

Navigation

[0] Message Index

Go to full version