【发布时间】:2013-04-26 08:29:43
【问题描述】:
我正在使用下面的代码来分隔奇数和偶数并将其存储在不同的变量中。当只有 2 个值可用时,它工作正常,但当数值增加时,它不会。我想让它动态化,以便可以正确分离和存储 n 个值。
示例: 如果
的值$final_array = "PNL testing 1,10,PNL testing 2,35,";
打印效果很好:
$teams = "PNL testing 1, PNL testing 2";
$amount = "10, 35";
但是当它从
$final_array = "PNL testing 1,10,PNL testing 2,35,";
到
$final_array = "PNL testing 1,10,PNL testing 2,35,Team 3,95,";
然后它也会打印出来
$teams = "PNL testing 1, PNL testing 2";
$amount = "10, 35";
请指导我了解我哪里出错了。
$res = array();
$result = preg_match_all("{([\w\s\d]+),(\d+)}", $final_array, $res);
$teams = join(', ', $res[1]); //will display teams
$amount = join(', ', $res[2]); //will display amount every team have
echo $teams . "<br />" . $amount;
【问题讨论】:
-
真的不明白这与偶数和奇数有何关系?
-
可能会在这里找到您想要的东西.. stackoverflow.com/questions/738168/filter-array-odd-even?rq=1
-
您提到的打印代码:
PNL testing 1, PNL testing 2, Team 3为$teams,10, 35, 95为$amount,不是您所说的打印代码。 -
所以基本上,如果团队数量是奇数,你想放弃 1 个团队?
标签: php regex arrays preg-match