0

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>';
}
Shushant
  • 1,625
  • 1
  • 13
  • 23
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

4 Answers4

3

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

Community
  • 1
  • 1
Donovan Charpin
  • 3,567
  • 22
  • 30
  • This is great. please can you show me how the code is changed if I combine more than two arrays? – EnexoOnoma Sep 26 '13 at 16:07
  • I never tried with more two array. I don't want put a wrong code without test it. Try this link http://stackoverflow.com/questions/6553752/array-combine-three-or-more-arrays-with-php. I don't think you are only with this problem :). – Donovan Charpin Sep 26 '13 at 16:15
  • Can you pass your post as solved if the answer has help you? Thank's – Donovan Charpin Sep 29 '13 at 12:32
1

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>';
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

may be this will help you out

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

     $array[] = array($match, $register[1][$i]);
}
var_dump($array);
Shushant
  • 1,625
  • 1
  • 13
  • 23
1

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/

Akshit Ahuja
  • 337
  • 2
  • 15
  • Your answer is valid, but I would suggest adding a code example of `array_merge()` (possibly using the scenario that the OP uses in the posted question) to make your answer truly great. Remember, links are great for tracking source, but answers should be able to still stand alone and stay completely valid in the event of your link becoming invalid. Good luck! – Frits Jul 25 '17 at 08:05
  • This is perfect. Like I said, it is now a much better quality answer, and will be much more useful to future/potential users who land on this question/answer after a google search. Thanks for the extra effort. – Frits Jul 25 '17 at 09:09