M
M
Makarqaz2014-02-15 08:43:51
PHP
Makarqaz, 2014-02-15 08:43:51

Is there an analogue of the choice (python) function in php?

There was a program in Python, now it needs to be transferred to PHP. PHP has just started to learn, so the question is: is there an analogue of the choice function from Python in it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Shishmintsev, 2014-02-15
@Makarqaz

If I understand correctly:
then in PHP it is like this

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 1);
echo $input[$rand_keys[0]] . "\n";

// или
shuffle($input);
echo $input[0] . "\n";

Z
ZloyHobbit, 2014-02-15
@ZloyHobbit

By the way, it's useful to smoke a manual on the subject of syntax:
us2.php.net/manual/ru/function.array-rand.php

<?php
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question