donglv9116 2014-07-17 15:18
浏览 321
已采纳

随机数组PHP的数组

I am trying to shuffle an array in PHP. I know there are several ways to do this, but I don't achieve it.

I have a PHP file which makes three queries to the DataBase. Every one of the query get elements and at the end I want to shuffle the array to watch the elements in a different order.

I show you my code.

 <?php

 // Parametros para conectar a la base de datos
 $username = "****";
 $password = "****";
 $hostname = "localhost"; 

 // Conectando, seleccionando la base de datos
 $link =  mysql_connect($hostname, $username, $password) or die('No se pudo conectar: ' . mysql_error());
 //echo 'Conectado satisfactoriamente';
 mysql_select_db('Agenda Juvenil') or die('No se pudo seleccionar la base de datos');

  mysql_query('SET CHARACTER SET utf8');

 // HACEMOS LA CONSULTA PARA EL PRIMER TEMA PREFERIDO Y SACAMOS HASTA 30 EVENTOS
 if (isset($_GET['temas_smultiple0'])) {
 $tema0 = $_GET['temas_smultiple0'];

 $query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate,   coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE    temas_smultiple IN ('$tema0') limit 0,15";        

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["eventos"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 //HACEMOS LA CONSULTA PARA EL SEGUNDO TEMA FAVORITO EN CASO DE QUE EXISTA, Y SACAMOS 20 EVENTOS
 if (isset($_GET['temas_smultiple1'])) {
 $tema1 = $_GET['temas_smultiple1'];

$query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate, coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE temas_smultiple IN ('$tema1') limit 0,10";      

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 //HACEMOS LA CONSULTA PARA EL TERCER TEMA FAVORITO, EN CASO DE QUE EXISTA, Y DEVOLVEMOS 10 EVENTOS
if (isset($_GET['temas_smultiple2'])) {
$tema2 = $_GET['temas_smultiple2'];

 $query = "SELECT id, title, barrio_smultiple, coordenadas_p_0_coordinate, coordenadas_p_1_coordinate, gratuita_b, temas_smultiple FROM eventosDiarios WHERE temas_smultiple IN ('$tema2') limit 0,5";      

$result = mysql_query($query) or die('Consulta fallida: ' . mysql_error());

if (mysql_num_rows($result) > 0) {
// looping through all results
// products node

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $evento = array();
        $evento["id"] = $row["id"];
        $evento["title"] = $row["title"];
        $evento["barrio_smultiple"] = $row["barrio_smultiple"];

        $evento["coordenadas_p_0_coordinate"] = $row["coordenadas_p_0_coordinate"];
        $evento["coordenadas_p_1_coordinate"] = $row["coordenadas_p_1_coordinate"];
        $evento["gratuita_b"] = $row["gratuita_b"];
        $evento["temas_smultiple"] = $row["temas_smultiple"];

        // push single product into final response array
        array_push($response["eventos"], $evento);
    }
   }
 }

 // success
$response["success"] = 1;

// echoing JSON response
 echo json_encode($response);

// Liberar resultados
mysql_free_result($result);


// Cerrar la conexión
 mysql_close($link);
?>

I have prooved with shuffle($response); and shuffle_assoc($response) but nothing..

Thanks in advance.

Regards.

  • 写回答

1条回答 默认 最新

  • drouie2014 2014-07-17 15:22
    关注

    I think you want to shuffle $response['eventos'] and not $response.

    shuffle($response['eventos'])
    

    .. since your main array is an associative array with few keys, where I'm pretty sure you don't care which order the elements are in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义