gpt4 book ai didi

php - 随机排列数组 PHP

转载 作者:行者123 更新时间:2023-11-29 06:35:57 26 4
gpt4 key购买 nike

我正在尝试在 PHP 中打乱数组。我知道有几种方法可以做到这一点,但我没有实现。

我有一个 PHP 文件,它对数据库进行了三个查询。每个查询都获取元素,最后我想打乱数组以按不同的顺序观察元素。

我给你看我的代码。

 <?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);
?>

我已经用 shuffle($response); 证明了和 shuffle_assoc($response) 但什么都没有..

提前致谢。

问候。

最佳答案

我想你想洗牌 $response['eventos'] 而不是 $response

shuffle($response['eventos'])

.. 因为你的主数组是一个有几个键的关联数组,我很确定你不关心元素的顺序。

关于php - 随机排列数组 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24807530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com