Mix random names and surnames in php

0

I have 3 arrays:

$nombres = array("Pepe", "Carlos", "Jesús", "Lola", "Rosa", "Maria");
$apellidos1 = array("Martin", "Lopez", "Salas", "Mateo", "Abas", "De Diego");
$apellidos2 = array("Quesada", "Alcala", "Marín", "Suarez", "Cobos","Rios");

and I want to create a third party that mixes names and names randomly, I tried to put the function mt_rand in the indexes but I do not do well, I have not achieved with functions that make this type of mixtures. It does not throw me an error, but the array shows me:

  

Random name 3rd random surname 0 second random last name 5

My current code:

<?php

$nombres = array("Pepe", "Carlos", "Jesús", "Lola", "Rosa", "Maria");
$apellidos1 = array("Martin", "Lopez", "Salas", "Mateo", "Abas", "De Diego");
$apellidos2 = array("Quesada", "Alcala", "Marín", "Suarez", "Cobos","Rios");

$randn = array_rand($nombres,5);
$radn2 = array_rand($apellidos1,5);
$rand3 = array_rand($apellidos2,5);
echo "Nombre aleatorio ". $randn[mt_rand(0,5)]. "apellido aleatorio ".$radn2[mt_rand(0,5)]."segundo apellido aleatorio ".$rand3[mt_rand(0,5)];
?>
    
asked by ras212 23.02.2017 в 23:14
source

1 answer

2

You do not need to pass, in the second parameter, the length of the array. That value is deducted array_rand by itself.

The effect of passing a number N other than 1 to the second parameter of array_rand, is that you will get an answer of N positions at random. If you pass a 1 or omit the parameter, it will return an integer, which I think is what you're looking for.

<?php

$nombres = array("Pepe", "Carlos", "Jesús", "Lola", "Rosa", "Maria");
$apellidos1 = array("Martin", "Lopez", "Salas", "Mateo", "Abas", "De Diego");
$apellidos2 = array("Quesada", "Alcala", "Marín", "Suarez", "Cobos","Rios");

$randn = array_rand($nombres);
$radn2 = array_rand($apellidos1);
$rand3 = array_rand($apellidos2);
echo "Nombre aleatorio ". $nombres[$randn] 
    ." apellido aleatorio ". $apellidos1[$radn2] 
    ." segundo apellido aleatorio ". $apellidos2[$rand3];
    
answered by 23.02.2017 / 23:26
source