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

我通过print_r在下面有一个数组,但是我只需要使用其中的一部分,所以我可以使用它 在flot chart中。</ p>


数组([success] =&gt; 1 [timestamp] =&gt; 1383824357 [data] =&gt;数组([0]
= &gt;数组([label] =&gt; customer1 [SUM(value)] =&gt; 12345)[1] =&gt;数组([label] =&gt; customer2 [SUM(value)] =&gt; 67890)[2 ] =&gt;数组([label] =&gt;
customer3 [SUM(value)] =&gt; 11223)[3] =&gt;数组([label] =&gt; customer4
[SUM(value)] = &gt; 33445)))</ p>
</ blockquote>

flot图表想要的格式是这样的(使用上面的数据):</ p>

  [
{label:“customer1”,data:12345},
{label:“customer2”,data:67890},
{label:“customer3”,data:11223},
{ label:“customer4”,数据:34455}
];
</ code> </ pre>

有谁知道如何访问上述数据?</ p> \ n </ div>

展开原文

原文

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?

doucitao2944
doucitao2944 根据您的要求准备新阵列或
接近 7 年之前 回复

4个回答



所以你基本上想用“SUM(value)”键改变“data”键然后访问数组? 如果是这样你可以使用这个片段:</ p>

 &lt;?php 
echo json_encode($ data);

function array_change_key_name($ orig,$ new,&amp; $ array ){
foreach($ array as $ k =&gt; $ v){
$ res [$ k === $ orig? $ new:$ k] =((is_array($ v)|| is_object($ v))?array_change_key_name($ orig,$ new,$ v):$ v);
}
返回$ res;
}

$ new = array_change_key_name(“data”,“SUM(value)”,$ data);
echo json_encode($ new);
?&gt;
</ code> </ pre>

编辑:代码更改键SUM(值)中的键数据...如果我理解你想要对应...只需反转这样的函数参数</ p>

< p> $ new = array_change_key_name(“SUM(value)”,“data”,$ data); </ p>

还有另一种方法可以做同样的事情,但你必须改变查询:</ p>

 选择SUM(值 )等等
</ code> </ pre>

到</ p>

 选择SUM(值)作为数据等等
</ code> </ pre>

并且数据库将处理所有的u:)</ p>
</ div>

展开原文

原文

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



只需将其转换为JSON对象:</ p>

  $ json = json_encode(  $ array ['data']); 
</ code> </ pre>

然后你就拥有了你需要的结构。</ p>

你可能需要 将字段名称从 value </ code>更改为 data </ code>以及。</ p>
</ div>

展开原文

原文

Just convert convert it to a JSON object:

$json = json_encode($array['data']);

Then you've got the structure you need.

You might need to change the field names from value to data aswell.



  print_r($ array ['data']);  // $ array是数组的名称
</ code> </ pre>

你可以这样做:</ p>

  foreach($ myArray as  $ val){
echo $ val ['data']。'&lt; br /&gt;';
//做你想做的任何事情
}
</ code> </ pre>
</ DIV>

展开原文

原文

print_r($array['data']); // $array is the name of your array

You can do:

foreach ($myArray as $val) {
    echo $val['data'].'<br />';
    // Do whatever you want to do here
}



试试这个可能它会起作用。 我没有测试过...... </ p>

  $ myArray =“你的数组”; 
$ data = $ myArray ['data'];
$ js_ar ='“ ';
foreach($ data as $ record)
{
$ js_ar。='{“label”:$ record [label],data:$ record [SUM(value)]}';

}
$ js_ar ='“';
$ json_data = json_encode($ js_ar);

print_r($ json_data);
</ code> </ pre>
</ div>

展开原文

原文

Try this May be it will Work . I didn't tested ...

 $myArray = "Your array";
 $data = $myArray['data'];
 $js_ar = '"';
    foreach($data as $record)
    {
        $js_ar .= '{ "label": $record[label],  data: $record[SUM(value)]}';

    }
    $js_ar = '"';
    $json_data = json_encode($js_ar);

    print_r ($json_data);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐