我如何创建MySQL查询这个数组? [英] How I create this array from mysql query?

查看:88
本文介绍了我如何创建MySQL查询这个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么可以创建这个数组

I want know how I can create this array

$datos = new ArrayIterator(array(
  "08:00:00" => new Vector ("08:00:00","08:30:00","sandrad"),
  "10:00:00" => new ArrayIterator (array( "10:00:00", "10:45:00", "palomas" )),
  "20:00:00" => new Vector("20:00:00","21:15:00","pedrales"),
  "25:30:00" => new ArrayIterator ( .....etc....
))

这是我的查询,以及如何我是尝试:

this is my query and how i am try:

$sql = "SELECT * FROM amigos WHERE fecha='$fecha' AND ORDER BY hora_inicio ASC ";
$result = mysql_query($sql) or die(mysql_error());

$totalRows = mysql_num_rows($result);


while ($row = mysql_fetch_assoc($result)) {
  switch ($funcion) {
    case 'new Vector (':
      $funcion='new ArrayIterator (array(';
      break;
    case 'new ArrayIterator (array(':
      $funcion ='new Vector (';
      break;
    default:
      $funcion ='new Vector (';
}

switch ($fin_funcion) {
  case '),':
    $fin_funcion = ')),';
    break;
  case ')),':
    $fin_funcion = '),';
    break;
  default:
    $fin_funcion = '),';
}

$datos=new ArrayIterator(array(
  $row['hora_inicio'] => $funcion.'"'.$row['hora_inicio'].'","'.$row['hora_final'].'","'.$row['nombre'].'"'.$fin_funcion



));

}

但我没有任何运气。任何想法?

but I am not having any luck. any ideas?

推荐答案

您是通过循环每次创建一个新的$ DATOS ArrayIterator。
如果你想要一个ArrayIterator,创建整个数组,然后对其进行初始化。

You are creating a new $datos ArrayIterator every time through the loops. If you want an ArrayIterator, create the entire array and then initialize it.

$datos = array();
while($row=mysql_fetch_assoc($result)){
    switch($funcion){
        case 'new Vector (':
            $funcion='new ArrayIterator (array(';
            break;
        case 'new ArrayIterator (array(':
            $funcion ='new Vector (';
            break;
        default:
            $funcion ='new Vector (';
    }

    switch($fin_funcion){
        case '),':
            $fin_funcion = ')),';
            break;
        case ')),':
            $fin_funcion = '),';
            break;
        default:
            $fin_funcion = '),';
    }

    $datos[$row['hora_inicio']] = $funcion.'"'.$row['hora_inicio'].
        '","'.$row['hora_final'].'","'.$row['nombre'].'"'.$fin_funcion;
}

$datos = new ArrayIterator($datos);

这篇关于我如何创建MySQL查询这个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆