Merge records in foreach

1

I'm doing a record listing and I'm having the following difficulty:

On my system I have two tables and I need to list their records in an interleaved way.

The location of the listing is the same. Only I can not make a foreach to list one and then list the other's records.

Sometimes (or randomly) I'll have to display a record of table 2 in the middle of the listing in table 1.

Imagine a system that is printing ads in the middle of the actual records. This is more or less what I need to do.

How do I do this?

    
asked by anonymous 15.02.2018 / 16:56

1 answer

1

You can add everything in an array and use the suffle function to mix content.

Example:

<?php

$registos = array("noticia1","noticia2", "noticia3");
$anuncios = array("anuncio1", "anuncio2", "anuncio3");

$lista = array_merge($registos, $anuncios);

shuffle($lista);

foreach ($lista as $linha){
    echo "$linha\n";
}

?>

See the code working at IDEONE

That is, first use the array_merge function that allows you to merge two arrays. Then to mix content use the suffle

    
15.02.2018 / 17:51