我正在使用下面的代码来分隔奇数和偶数并将其存储在不同的变量中。当只有 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;