Spidey Spidey - 1 month ago 10
PHP Question

outer variable from nested loop

At the moment, i have:

$lines = ['0747890304', '0748906475', '0749104331', '0741233114'];
$inputs = ['074589', '074789', '074910'];
$output = [];

foreach($lines as $keyLine => $line){
foreach($inputs as $inputLine => $input){
if(strpos($line, $input) !== false){
$output[] = $line;
}
}
}
var_dump($output); // i got only the last loop. Variable gets overriden


And I want a list of
$lines
that contains and start with
$inputs
. I'm stuck.

Answer

This Loop of yours functions as it should. Quick-Test Here.

    $lines  = ['0747890304', '0748906475', '0749104331', '0741233114'];
    $inputs = ['074589', '074789', '074910'];
    $output = [];

    foreach($lines as $keyLine => $line){
        foreach($inputs as $inputLine => $input){
            if(strpos($line, $input) !== false){
                $output[] = $line;
            }
        }
    }
    var_dump($output);
    // YIELDS::
    array (size=2)
      0 => string '0747890304' (length=10)
      1 => string '0749104331' (length=10)
Comments