ideas:
- confirm the location of matchArr in oddEven, such as 1mem10 and 20
- according to the location obtained above, take out the data in number, and finally splice
.
implementation:
$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));
$matchArrString = implode($matchArr, ',');//1,2,2,2
$oddEvenString = implode($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
$strlen = strlen($matchArrString);//
//$matchArrString $oddEvenString
$strpos_1 = strpos($oddEvenString, $matchArrString);
$offset_1 = $strlen+$strpos_1;//
$strpos_2 = strpos($oddEvenString, $matchArrString, $offset_1);//2
$offset_2 = $strlen+$strpos_2;//2
$strpos_3 = strpos($oddEvenString, $matchArrString, $offset_2);//3
//
$index_2 = count(explode(',', substr($oddEvenString, 0, $strpos_2)))-1;
$index_3 = count(explode(',', substr($oddEvenString, 0, $strpos_3)))-1;
$count = count($matchArr);
for ($i=0; $i < $count; $iPP) {
$numbers_1[$i] = $numbers[$strpos_1+$i];
}
for ($i=0; $i < $count; $iPP) {
$numbers_2[$i] = $numbers[$index_2+$i];
}
for ($i=0; $i < $count; $iPP) {
$numbers_3[$i] = $numbers[$index_3+$i];
}
print_r($numbers_1);
echo '<br/>';
print_r($numbers_2);
echo '<br/>';
print_r($numbers_3);