【问题标题】:How I create this array from mysql query?如何从 mysql 查询创建这个数组?
【发布时间】:2011-03-15 04:53:51
【问题描述】:

我想知道如何创建这个数组

$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....
))

这是我的查询以及我的尝试:

$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



));

}

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

【问题讨论】:

  • 编程与运气无关,全靠手艺。

标签: php mysql arrays sorting


【解决方案1】:

每次循环都在创建一个新的 $datos ArrayIterator。 如果你想要一个 ArrayIterator,创建整个数组然后初始化它。

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

【讨论】:

  • 不,不工作..我还在尝试,谢谢!我真的很高兴你关心我的问题。
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 2016-07-29
  • 2019-08-24
  • 2015-11-24
相关资源
最近更新 更多