By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,117 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,117 IT Pros & Developers. It's quick & easy.

Split a variable into an Array

P: 1
I'm trying to split a list of numbers separated by a comma into an array, which i can then loop through and execute a command for each number in the list.

So what i really want to achieve is this;

$numbers = "2564521,5451254"
$data = array($numbers)

However this will not work with an array.

This is what i have so far;

Expand|Select|Wrap|Line Numbers
  1. $data = array(2564521,5451254);
  2.  
  3. //Our 'stepping' variable
  4. $g = 0;
  5.  
  6. //Our rowcount
  7. $rowcount = 0;
  8.  
  9. echo "<table cellspacing='0'>\r";
  10.     for ($i=0; $i<count($data); ) {
  11.  
  12.         $rowcount++;
  13.         echo "    <tr>\r"; //New row
  14.  
  15.         $g = $i + 3; //Set our nested limit
  16.         for( ; $i<$g; $i++) { //nested for loop
  17.  
  18.             if (!isset($data[$i])) { //Allow us to break on incomplete rows
  19.                 break;
  20.             }
  21.  
  22.             echo "        <td style='border: 1px #000 solid;'>\r"; //Out put a cell
  23.             echo "         Number: $data[$i]</p>\r";
  24.             echo "        </td>\r";
  25.             echo "$data[$i]</p>\r";
  26.  
  27.             $tonumbers = "$data[$i]";
  28.  
  29. //Execute command for each number
  30.  
  31.  
  32.         }
  33.  
  34.         echo "    </tr> \r"; //End New Row
  35.     }
  36.  
  37. echo "</table>\r";?>
I would appreciate any help, thanks!
Jan 15 '12 #1
Share this Question
Share on Google+
2 Replies


P: 93
$numbers = "2564521,5451254";

$temp_array = explode(",",$numbers);

you can get $temp_array[0] = 2564521 and $temp_array[1] = 5451254
Apr 16 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.