我有一个循环多次循环,$x as $amount
在这种情况下。在循环内部,我有另一个foreach使用preg_match_all来匹配来自$text as $catch
的一些数据并将它们抛出到一个数组中以便在别处使用,在这种情况下我们将其设置为$instructions
。
我们现在将每个循环中的两个数据发送到一个函数passData($instructions, $amount)
。
问题:
当我们从passData()
循环内部调用$x
函数时,与$instructions
数组匹配的数据会在每次迭代时保持重复,如下所示,但结果为I&#39 ; m正在寻找不将相同的数据传递到下一个数组,遵循此示例,每个数组应包含相同数量的键0 - 4,而不是0 - 9.
Array is:1
Array
(
[0] => first paragraph goes here
[1] => http://www.google.com/g_logo.jpg
[2] => second paragraph is also here
[3] => third paragraph is much longer then the rest
[4] => http://www.facebook.com/f_logo.jpg
)
Array is:2
Array
(
[0] => first paragraph goes here
[1] => http://www.google.com/g_logo.jpg
[2] => second paragraph is also here
[3] => third paragraph is much longer then the rest
[4] => http://www.facebook.com/f_logo.jpg
[5] => first paragraph goes here
[6] => http://www.google.com/g_logo.jpg
[7] => second paragraph is also here
[8] => third paragraph is much longer then the rest
[9] => http://www.facebook.com/f_logo.jpg
)
我尝试了什么:
我能想到的唯一解决方案是在$text
之后调用unset(),但它没有解决问题。
这是我写的代码:
function passData($a, $b){
echo '<pre>';
echo 'Array is:' . $b, '</br>';
print_r($a);
echo '</pre>';
}
function tryFunc(){
$x = ['1','2'];
foreach($x as $amount){
$text = [
'1. first paragraph goes here',
'<img src="http://www.google.com/g_logo.jpg">',
'2. second paragraph is also here',
'3. third paragraph is much longer then the rest',
'<img src="http://www.facebook.com/f_logo.jpg">'
];
foreach($text as $catch){
if(preg_match_all("/(?:\d)\. (.*)/", $catch, $output_array)) {
foreach($output_array[1] as $instructions_output){
$instructions[] = $instructions_output;
}
}
if(preg_match_all("/<*img[^>]*src *= *[\"\']?([^\"\']*)/", $catch, $cought_array)) {
$instructions[] = $cought_array[1][0];
}
}
if(passData($instructions, $amount)){
echo 'passed';
}
}
}
tryFunc();