问题描述有三个数组,如下:// 数字数组$numbers = ['17', '38', '12', '40', '20', '47', '45', '22', '02', '19', '29', '10', '27', '41', '26', '47', '18', '24', '22', '27', '33', '47', '25', '09', '16', '32', '01', '28', '18', '38'];// 单双数组$oddEven = ['1', '2', '2', '2', '2', '1', '1', '2', '2', '1', '1', '2', '1', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1', '1', '2', '2', '1', '2', '2', '2'];// 匹配数组$matchArr = ['1', '2', '2', '2'];现在我想用$matchArr匹配$oddEven,请问下怎么才能把$oddEven中的3个['1', '2', '2', '2']给匹配出来?同时能对应上$numbers中的数字?我希望的结果是:$result[0] = ['17', '38', '12', '40'];
$result[1] = ['47', '18', '24', '22'];
$result[2] = ['01', '28', '18', '38'];或者合并在一起也可以,可以有重复的值:$result = ['17', '38', '12', '40', '47', '18', '24', '22', '01', '28', '18', '38'];我试过把数组转成字符串使用preg_match_all()进行匹配,但是这样就无法与$numbers对应了。实在想不到什么办法了,请大家帮帮我,谢谢了。
1 回答
手掌心
TA贡献1942条经验 获得超3个赞
思路:
确认matchArr在oddEven的位置,比如1,10,20
根据上面获取的位置,取出number里的数据,最后拼接
实现:
$numbers = ['17', '38', '12', '40', '20', '47', '45', '22', '02', '19', '29', '10', '27', '41', '26', '47', '18', '24', '22', '27', '33', '47', '25', '09', '16', '32', '01', '28', '18', '38'];// 单双数组$oddEven = ['1', '2', '2', '2', '2', '1', '1', '2', '2', '1', '1', '2', '1', '1', '2', '1', '2', '2', '2', '1', '1', '1', '1', '1', '2', '2', '1', '2', '2', '2']; $oddEvenStrs = implode("", $oddEven);// 匹配数组$matchArr = ['1', '2', '2', '2']; $matchArrStrs = implode("", $matchArr); var_dump($oddEvenStrs); var_dump($matchArrStrs); var_dump(count($numbers));function findIt($from, $find, $numbers, $pos) { $result = ''; $pos = strpos($from, $find, $pos); if ($pos !== false) { $result .= implode(",", array_slice($numbers, $pos, 4)); $pos += 4; } if (count($numbers) > $pos) { return $result . "," . findIt($from, $find, $numbers, $pos); } return $result; } $result = findIt($oddEvenStrs, $matchArrStrs, $numbers, 0);echo $result; var_dump(explode(",", $result));
- 1 回答
- 0 关注
- 723 浏览
添加回答
举报
0/150
提交
取消