Random elements of an array

1

I have problems with this code that I try to execute:

$buscar = array("necesito 01","necesito 02","necesito 03");

$resul = $buscar[mt_rand(0, count($buscar) - 1)];
    $query = array(
        "q" => "'+$resul+'",
            "count" => 4,
            "result_type" => "recent",  
    );

It does not give me the desired result what I need is that the variable $ results that is inside the array is random but it gives me an error.

    
asked by BotXtrem Solutions 07.07.2017 в 08:06
source

2 answers

3

You could use array_rand , but in practice it's a slower and less random function than the following code:

<?php

    $buscar = array("necesito 01","necesito 02","necesito 03");

    $result = $buscar[mt_rand(0, count($buscar) - 1)];

    $query = array(
        // "q" => "'+" . $result. "+'", // Concatenación
        "q" => "'+{$result}+'", // Magic Quotes
        "count" => 4,
        "result_type" => "recent",  
    );
    
answered by 07.07.2017 / 08:28
source
0

If I understand correctly that your problem is to collect $result in the array for the query, try:

$query = array(
        "q" => $result,
            "count" => 4,
            "result_type" => "recent",  
);

would be the correct way to call the variable.

    
answered by 07.07.2017 в 10:49