DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: [PHP] Variables in array to int ?  (Read 5,153 times)

RedPillow

  • Pillows.
  • Member
  • Joined in 2008
  • **
  • Posts: 141
[PHP] Variables in array to int ?
« on: December 09, 2010, 06:19 AM »
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) } 
« Last Edit: December 09, 2010, 06:50 AM by RedPillow »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • Participated in NANY Challenge Eight Times
    • Programming School - Deluxe Bonus Medal
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Military Service Badge - US Army
    • Writer
Re: [PHP] Variables in array to int ?
« Reply #1 on: June 20, 2011, 12:56 AM »
Rather than the following...

Code
$num = (int)$str;

...you need something like this...

Code
$num = intval($str);

The former looks more like casting in C#.
Win 10 Pro running on a 3.2 GHz CPU and 12 Gb RAM
__________________________________________

Proofreading Service: http://williammillerservices.com/proofreading

Original address: https://www.donationcoder.com/forum/index.php?topic=24828.0