1

I'm trying to get a PHP script 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;

$data = array(2564521,5451254);

//Our 'stepping' variable
$g = 0;

//Our rowcount
$rowcount = 0;

echo "<table cellspacing='0'>\r";
    for ($i=0; $i<count($data); ) {

        $rowcount++;
        echo "    <tr>\r"; //New row

        $g = $i + 3; //Set our nested limit
        for( ; $i<$g; $i++) { //nested for loop

            if (!isset($data[$i])) { //Allow us to break on incomplete rows
                break;
            }

            echo "        <td style='border: 1px #000 solid;'>\r"; //Out put a cell
            echo "         Number: $data[$i]</p>\r";
            echo "        </td>\r";
            echo "$data[$i]</p>\r";

            $tonumbers = "$data[$i]";

//Execute command for each number


        }

        echo "    </tr> \r"; //End New Row
    }

echo "</table>\r";?>

I would appreciate any help, thanks! 
0

2 Answers 2

6

Looks like PHP, if so you can do

$data = explode(",", $numbers);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, although i did try that and all i got was the first number and only the first digit of the second number, I'm not sure why though?
If you have a string $numbers = "2564521,5451254", then it should split those numbers into an array with explode. What is the data you are working with?
2

I am a bit confused by your code. Let me see if this is what you are looking for.

<?php

$testdata = "123123123123,34234804982";

$data = explode(",", $testdata);

foreach($data as $number) {
    echo $number . '|';
}

?>

1 Comment

I am glad. Please mark this as a accepted answer. It's the check-mark next to the up arrow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.