topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Wednesday April 17, 2024, 8:52 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

RedPillow

  • Member
  • Joined in 2008
  • **
  • Posts: 141
  • Pillows.
    • View Profile
    • Read more about this member.
    • Donate to Member
[PHP] Variables in array to int ?
« on: December 09, 2010, 12: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 [Select]
  1. if (isset($_GET["num"])) { foreach
  2. ($_GET["num"] as $arvo) $num = $num . $arvo . ",";
  3. $numchk[] = $num;}
  4. $numchktostring = $numchk[0];
  5. $num2 = explode(",", $numchktostring);
  6.  
  7. unset($num2[7]);
  8. $validchk = count($num2);

Now, it somewhy gives me output like this (By var_dumping $num2):
Code: PHP [Select]
  1. 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 [Select]
  1. $str = "10";
  2.       $num = (int)$str;

-- EDIT --

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

Code: PHP [Select]
  1. $i = 0;
  2. $a = 0;
  3. While ($i < 7) {
  4. $num2[$a] = (int)$num2[$a];
  5. $i++;
  6. $a++;
  7. }

var_dump($num2) outputs now:
Code: PHP [Select]
  1. 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, 12:50 AM by RedPillow »

kyrathaba

  • N.A.N.Y. Organizer
  • Honorary Member
  • Joined in 2006
  • **
  • Posts: 3,200
    • View Profile
    • Donate to Member
Re: [PHP] Variables in array to int ?
« Reply #1 on: June 19, 2011, 07:56 PM »
Rather than the following...

$num = (int)$str;

...you need something like this...

$num = intval($str);

The former looks more like casting in C#.