8795 sujets

Développement web côté serveur, CMS

Coucou tout le monde,

Voila, un de mes clients prefere que j'utilise les tableaux plutot qu'une base de donnees pour le listing de ses services. Le seul truc, c'est que j'ai jamais eu l'occasion d'utiliser les tableaux, je sais ca peu paraitre bizarre.

Donc mon but est d'afficher aleatoirement un service (nom + description).
Et voici mon code qui m'affiche juste "array", j'aimerais savoir ou est mon erreur.


$tab_services = array(
array('Corporate Events', 'Warbird Flights', 'Sit in the Spitfire', 'Nightshoots', 'Film and TV work'),
array('Corporate Events Caption', 'Warbird Flights Caption', 'Sit in the Spitfire Caption', 'Nightshoots Caption', 'Film and TV work Caption')
);
$random = array_rand($tab_services, 2);
echo $tab_services[$random[0]] . "\n";


Merci de votre aide !
Bonjour Fanny,

Il y a peut-être des solutions plus directes et efficaces, mais j'ai le tort de toujours penser compliqué en premier lieu :

Ce code-ci semble fonctionner :

$tab_services = array(
array('Corporate Events', 'Warbird Flights', 'Sit in the Spitfire', 'Nightshoots', 'Film and TV work'),
array('Corporate Events Caption', 'Warbird Flights Caption', 'Sit in the Spitfire Caption', 'Nightshoots Caption', 'Film and TV work Caption')
);
$entrees_tab = count($tab_services[0]);
$aleatoire = mt_rand(0,$entrees_tab);
echo $tab_services[0][$aleatoire] . ':' . $tab_services[1][$aleatoire];


Si tu voulais maintenir l'utilisation d'array_rand, il fallait parcourir le tableau de ton tableau, apparemment. Ceci semble fonctionner aussi, mais je n'ai pas été plus loin dans cette direction :

$random = array_rand($tab_services[0]);
var_dump($random);


Bonne chance.
Modifié par Reka (05 Mar 2014 - 17:08)