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 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接