問(wèn)題描述有三個(gè)數(shù)組,如下:// 數(shù)字?jǐn)?shù)組$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'];// 單雙數(shù)組$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'];// 匹配數(shù)組$matchArr = ['1', '2', '2', '2'];現(xiàn)在我想用$matchArr匹配$oddEven,請(qǐng)問(wèn)下怎么才能把$oddEven中的3個(gè)['1', '2', '2', '2']給匹配出來(lái)?同時(shí)能對(duì)應(yīng)上$numbers中的數(shù)字?我希望的結(jié)果是:$result[0] = ['17', '38', '12', '40'];
$result[1] = ['47', '18', '24', '22'];
$result[2] = ['01', '28', '18', '38'];或者合并在一起也可以,可以有重復(fù)的值:$result = ['17', '38', '12', '40', '47', '18', '24', '22', '01', '28', '18', '38'];我試過(guò)把數(shù)組轉(zhuǎn)成字符串使用preg_match_all()進(jìn)行匹配,但是這樣就無(wú)法與$numbers對(duì)應(yīng)了。實(shí)在想不到什么辦法了,請(qǐng)大家?guī)蛶臀?,謝謝了。
1 回答

手掌心
TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
思路:
確認(rèn)matchArr在oddEven的位置,比如1,10,20
根據(jù)上面獲取的位置,取出number里的數(shù)據(jù),最后拼接
實(shí)現(xiàn):
$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'];// 單雙數(shù)組$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);// 匹配數(shù)組$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 關(guān)注
- 757 瀏覽
添加回答
舉報(bào)
0/150
提交
取消