更改字符串内容的顺序

时间:2015-07-30 22:27:47

标签: php

我有这个数据

$s = '{ "date": "2015-07-30","value": 66568 },{ "date": "2015-07-29","value": 66598 },{ "date": "2015-07-28","value": 66680 },{ "date": "2015-07-27","value": 66774 }';

我正在绘制数据图,基本上我需要图表来显示最早的日期,如果我只是翻转数据就可以实现。我尝试使用:

implode(' ', array_reverse(explode(',', $s)))

那并没有用。

数据来自数据库并附加到

if($test!=1) {
    $testString = $testString.',{ "date": "'.date("Y-m-d",strtotime($date)).'","value": '.$res[$row]['followed_by'].' }';  
} else {
    $testString = $testString.'{ "date": "'.date("Y-m-d",strtotime($date)).'","value": '.$res[$row]['followed_by'].' }';  
}

使用"dataProvider": [ <?php echo $testString; ?> ]

将数据添加到图表中

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码:

$s = '{ "date": "2015-07-30","value": 66568 },{ "date": "2015-07-29","value": 66598 },{ "date": "2015-07-28","value": 66680 },{ "date": "2015-07-27","value": 66774 }';

function reverse_str($str) {
  $decoded = json_decode(sprintf('[%s]', $str), true);
  $reversed = array_reverse($decoded);
  return trim(json_encode($reversed), '[,]');  
}

Usage:
$newStr = reverse_str($s);