0

从查询的数据库中,我们得到了这个

foreach ($child_posts as $child_post) {
 $child_id = $child_post->ID;
 $dayOfWeek = get_post_meta($child_id,'wpcf-day-of-week', true);
 $time = get_post_meta($child_id,'wpcf-time', true);

$class[] = array('day' => $dayOfWeek, 'time' => $time, 'value' => $child_id);
}

{"classes":[
{"day":"7","time":"1500","value":13574},
{"day":"7","time":"1800","value":13573},
{"day":"4","time":"1900","value":11346},
{"day":"6","time":"1100","value":11494},
{"day":"5","time":"1800","value":11362},
{"day":"7","time":"1700","value":13572},
{"day":"6","time":"1600","value":11498},
{"day":"6","time":"1500","value":11496}]}

日 = 周一 - 周日

时间=军用时间

价值 = 我需要但不需要排序的东西

我需要先按天排序,然后按时间排序。并以原来的格式取回数据库

{"classes":[
{"day":"4","time":"1900","value":11346},
{"day":"5","time":"1800","value":11362},
{"day":"6","time":"1100","value":11494},
{"day":"6","time":"1500","value":11496},
{"day":"6","time":"1600","value":11498},
{"day":"7","time":"1500","value":13574},
{"day":"7","time":"1700","value":13572},
{"day":"7","time":"1800","value":13573}]}

我尝试使用 php 手册中的方法,但我仍然无法真正得到它。

这就是我所做的

foreach ($class as $key => $row) {
    $day[$key]  = $row['day'];
    $time[$key] = $row['time'];
    $value[$key] = $row['value'];

}

$class[] = array_multisort($day, SORT_DESC, $time, SORT_ASC, $class);

我知道我应该期待什么,但我不知道如何去获得它。希望有人可以在这里提供帮助:)

我的原始脚本:

 $childargs = array(
 'post_type' => 'class',
 'numberposts' => -1,
 'meta_query' => array(array('key' => '_wpcf_belongs_instructor_id', 'value' => $instructor_post_id))
 );
 $child_posts = get_posts($childargs);

 //$child_posts = types_child_posts(‘class’);
 foreach ($child_posts as $child_post) {
  $child_id = $child_post->ID;
  $dayOfWeek = get_post_meta($child_id,'wpcf-day-of-week', true);
  $time = get_post_meta($child_id,'wpcf-time', true);

 $class[] = array('day' => $dayOfWeek, 'time' => $time, 'value' => $child_id);
 }

 echo json_encode(
   array("classes" => $class)
 )
 ?>
4

2 回答 2

0
<?php

$json = '{"classes":[
          {"day":"7","time":"1500","value":13574},
          {"day":"7","time":"1800","value":13573},
          {"day":"4","time":"1900","value":11346},
          {"day":"6","time":"1100","value":11494},
          {"day":"5","time":"1800","value":11362},
         {"day":"7","time":"1700","value":13572},
         {"day":"6","time":"1600","value":11498},
       {"day":"6","time":"1500","value":11496}]}';


$array = json_decode($json,true);

$temp = $array['classes'];

foreach($temp as $key=>$value)
{
  $day[] = $value['day'];
  $time[] = $value['time'];
  }

array_multisort($day,SORT_ASC,$time,SORT_ASC,$temp);

$array['classes'] = $temp;

echo json_encode($array);
?>

尝试将您的代码更改为。我假设 $classes 包含 json_decoded 内容。

$temp = $classes['classes'];

foreach ($temp as $key => $row) {
$day[]  = $row['day'];
$time[] = $row['time'];
$value[] = $row['value'];
}

 array_multisort($day, SORT_DESC, $time, SORT_ASC, $temp);
 $classes['classes'] = $temp;

 echo json_encode($classes);
于 2013-03-27T16:22:55.297 回答
0

按一列对从 MYSql 返回的行数组进行排序。这里的问题是按会话 (1,2) 和周期 (1,2,3) 对课程进行排序 如果会话 = 3 这意味着如果会话 == 3 准备表格以进行排序和打印,则插入第 1 行, 和第 2 行。然后进行排序。表格内容在 $coursesr 中。

$ar = array();
if ($order == "session"){
    for ($i = 0; $i < count($coursesr); $i++){          
        $ar[0][] = (($coursesr[$i]['session'])*10)+$coursesr[$i]['period'];
        $ar[1][] = $i;
    }
    $b = array_multisort($ar[0],SORT_ASC,SORT_NUMERIC,$ar[1],SORT_ASC,SORT_NUMERIC);        
}else{
    for ($i = 0; $i < count($coursesr); $i++){          
        $ar[0][] = ucwords($coursesr[$i]['lastname']);
        $ar[1][] = $i;
    }
    array_multisort($ar[0],SORT_ASC,SORT_NATURAL,$ar[1],SORT_ASC,SORT_NUMERIC);
}

然后要将其从 $coursesr 数组中取出,您必须计算索引...

for ($i = 0; $i < $num; $i++){
    $r = $coursesr[$ar[1][$i]]; // simplify reference   
    echo "<tr>";.....
于 2013-11-22T15:53:39.410 回答