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条)

报告相同问题?

悬赏问题

  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。