dongshilve4392 2012-01-15 23:42
浏览 23
已采纳

将变量拆分为数组

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'>";
    for ($i=0; $i<count($data); ) {

        $rowcount++;
        echo "    <tr>"; //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;'>"; //Out put a cell
            echo "         Number: $data[$i]</p>";
            echo "        </td>";
            echo "$data[$i]</p>";

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

//Execute command for each number


        }

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

echo "</table>";?>

I would appreciate any help, thanks! 
  • 写回答

2条回答

  • douzheng0702 2012-01-16 01:08
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错