Printing items from an associative array in random order [closed]

0

I am creating a merry-go-round, where you will have 3 sliders containing 15 images within each one, totaling 45 images.

I'vecreatedanassociativearraytodisplaytheseimagesrandomly,sofarsogood,however,nowIneedthefirst15itemstobefixed,whereIcanchoosewhichindexesaredisplayedfirstandtheother30aredisplayedrandomlybutwithoutrepeatingtheonesIchose.

$clientes=array(array("nome" => "Cliente 1",
    "categoria" => "Turismo",
    "logo" => "turismo.jpg"
),
array(
    "nome" => "Suporte",
    "categoria" => "Tecnologia",
    "logo" => "suporte.jpg"
),
array(
    "nome" => "Faculdade Futura",
    "categoria" => "Educação",
    "logo" => "faculdade-futura.jpg"
),

shuffle($clientes);
foreach (array_slice($clientes, 0, 45) as $atributo => $valor):
   echo "{$valor["nome"]}";
endforeach;
);

This my array has more than 60 items, where it is displayed elsewhere on the site, but on the carousel I only need 45, so I used array_slice() .

    
asked by anonymous 22.11.2018 / 19:37

1 answer

1

If I understand correctly, I imagine this to work:

$tmp = $clientes;
shuffle(array_slice(tmp, 15, 30));
$novo = array_merge(array_slice($clientes, 0, 15), $tmp);
foreach ($novo as $atributo => $valor):
   echo "{$valor["nome"]}\n";
endforeach;

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

I just shuffled the sixteenth item by picking 30 (I may not have understood that part, I might need all the elements minus 15, if that's the case I can change the answer) items and concatenei with the 15 initials that do not form shuffled.

Maybe this is (I change if the AP clarifies):

$tmp = $clientes;
shuffle(array_slice(tmp, 15, count($clientes) - 15));
$novo = array_merge(array_slice($clientes, 0, 15), array_slice($tmp, 0, 30));
foreach ($novo as $atributo => $valor):
   echo "{$valor["nome"]}\n";
endforeach;

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

    
22.11.2018 / 19:49