Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, how to make no dublicate text? :))

PHP
function getKlausimaiRandom()
{
    $arrQuote = array();
    $arrQuote[0] = '
    <form method="post" action="" class="input_form">
        <h2>Ar var yra geras dalykas</h2>
        <input type="text" name="klausimas_1">
        <button type="submit" name="submit_1">Add Task</button>
    </form> ';
    $arrQuote[1] = '
    <form method="post" action="" class="input_form">
        <h2>Ar int yra geras dalykas</h2>
        <input type="text" name="klausimas_2">
        <button type="submit" name="submit_2">Add Task</button>
    </form> ';
    return $arrQuote[array_rand($arrQuote)];
}


What I have tried:

..............................................
Posted
Updated 29-Jan-18 0:56am
Comments
Mohibur Rashid 28-Jan-18 18:21pm    
What's expected to happen and what is happening?

Based on what presented, not sure if that possible, but you can write the code to return unique value

PHP
 $arrQuote = array();
    $arrQuote[0] = '111';
    $arrQuote[1] = '222';
    $arrQuote[2] = '222';
    
    // Deleting the duplicate items

    $result = array_unique($arrQuote);

    print_r($result);

return $result[array_rand($result)]; //return 111 or 222


Output: Array ( [0] => 111 [1] => 222 )

How to Remove Duplicate Values from an Array in PHP[^]
 
Share this answer
 
Because you are using PHP, you can use a trick not available in most languages - creating array elements indexed 'arbitrarily' at run-time.


When you generate a value, create the array element indexed with that value. BUT, before you create the value you test it to determine if it exists. If it already exists you need not save (although saving wouldn't make a difference since the value is the same).
PHP
$arrQuote[111] = '111';
$arrQuote[222] = '222';

if you a specific size for the array, test its length and stop producing entries when you have enough.

Also - remember that with PHP you can have character-based indices - so it works with alpha strings as well. If the order of generation is important, create each array element as a 2-dimensional value, one of the values being the creation order, the other, the value.

Go through the array with foreach() and the indices don't matter, and if you need special sorting, sort the array before the foreach() analysis.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900