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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?