duanlan3598 2013-11-07 11:41
浏览 25
已采纳

如何访问阵列的某些部分以用于flot图表?

I've got an array below via print_r, however I need to only use part of this so I can use it in flot charts.

Array ( [success] => 1 [timestamp] => 1383824357 [data] => Array ( [0] => Array ( [label] => customer1 [SUM(value)] => 12345) [1] => Array ( [label] => customer2 [SUM(value)] => 67890) [2] => Array ( [label] => customer3 [SUM(value)] => 11223 ) [3] => Array ( [label] => customer4 [SUM(value)] => 33445 ) ) )

The format that flot charts want is like so (with the above data used):

[
    { label: "customer1",  data: 12345},
    { label: "customer2",  data: 67890},
    { label: "customer3",  data: 11223},
    { label: "customer4",  data: 34455}
];

Does anyone know how I can go about accessing the data as above?

  • 写回答

4条回答

  • dsa80833 2013-11-07 11:54
    关注

    so u basically want to change the "data" key with the "SUM(value)" key and then accessing the array? if so u can use this snippet:

    <?php
    echo json_encode($data);
    
    function array_change_key_name( $orig, $new, &$array ) {
      foreach ( $array as $k => $v ) {
        $res[ $k === $orig ? $new : $k ] = ( (is_array($v)||is_object($v)) ? array_change_key_name( $orig, $new, $v ) : $v );
      }
      return $res;
    }
    
    $new = array_change_key_name("data" ,"SUM(value)" , $data);
    echo json_encode($new);
    ?>
    

    EDIT: The code change the key data in the key SUM(value) ... if i understand well u want the opposit ... just invert the function parameters like this

    $new = array_change_key_name("SUM(value)" ,"data" , $data);

    there is also another way to do the same thing, but u will have to change the query from:

    Select SUM(value) etc etc
    

    to

    Select SUM(value) as data etc etc
    

    and the DB will handle all for u :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料