EnexoOnoma
EnexoOnoma

Reputation: 8836

How to combine these two arrays and echo them in PHP?

How can I combine the following arrays? For example the first $match with the first $register and after that, to echo the array

$matches = array();
$registration = array();

preg_match_all('#(69\d{8}|21\d{8}|22\d{8}|23\d{8})#', $str2, $matches); 
preg_match_all('!<td class="registration">(.*?)</td>!is', $str2, $registration);


foreach ($matches[1] as $match) {
    echo $match.'<br>';
}
foreach ($registration[1] as $register) {
    echo $register.'<br>';
}

Upvotes: 0

Views: 238

Answers (4)

Akshit Ahuja
Akshit Ahuja

Reputation: 337

you can use array_merge() function .

$combinedArray = array_merge($matches, $registration);

foreach ($combinedArray as $row) {

}

https://codeupweb.wordpress.com/2017/07/14/merging-and-sorting-arrays-in-php/

Upvotes: 1

Shushant
Shushant

Reputation: 1635

may be this will help you out

$array = array();
foreach ($matches[1] as $key => $match) {

     $array[] = array($match, $register[1][$i]);
}
var_dump($array);

Upvotes: 1

Donovan Charpin
Donovan Charpin

Reputation: 3397

Try with this example :

foreach (array_combine($matches[1], $registrations[1]) as $matche => $registration) {
        echo $matche." - ".$registration;
    }

and an other post like as your : Two arrays in foreach loop

Upvotes: 3

gen_Eric
gen_Eric

Reputation: 227280

You can loop through one and get the same key from the other array.

foreach ($matches[1] as $key=>$match) {
    $register = $register[1][$key];

    echo $match.' '.$register.'<br>';
}

Upvotes: 1

Related Questions