Rumah > pembangunan bahagian belakang > tutorial php > 数组处理问题,求优化

数组处理问题,求优化

WBOY
Lepaskan: 2016-06-23 13:42:32
asal
931 orang telah melayarinya

有一个这样查询出来的数组。

$result = [	['month'=>08,'price'=>218],	['month'=>12,'price'=>140],];
Salin selepas log masuk


最终需要转换成一个字符串,用于前台js
格式类似:[49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, null, null]
显示的是每月的销售情况,没有值就为null
比如一月份的price为49.9

我的做法:
感觉有点麻烦,求优化

        //先构造类似  ['01'=>0,'02'=>0 .... '12'=>0]  这种格式的数组        $fullMonth = [];        for($i=1;$i<=12;$i++){            $fullMonth[str_pad($i,2,'0',STR_PAD_LEFT)] = 0;        }        //  遍历数组  对应月份有值就放到新建的数组里        $i = 0;        foreach($fullMonth as $month=>$value){            foreach($result as $record){                if($month == $record['month']){                    $fullMonth[$month] = $record['price'];                }            }            $i++;        }        return '[' . implode(',',$fullMonth) . ']';
Salin selepas log masuk


回复讨论(解决方案)

为什么不用json格式?

$result = [    ['month'=>08,'price'=>218],    ['month'=>12,'price'=>140],];echo json_encode($result); // [{"month":0,"price":218},{"month":12,"price":140}]
Salin selepas log masuk


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <meta http-equiv="content-type" content="text/html; charset=utf-8">  <title> New Document </title> </head> <body>  <?php    $result = [        ['month'=>08,'price'=>218],        ['month'=>12,'price'=>140],    ];  ?>  <div id="result"></div>  <script type="text/javascript">  var result = <?php echo json_encode($result); ?>;  var tmp = '';  for(var i=0; i<result.length; i++){      tmp += result[i].month + ' = ' + result[i].price + '<br>';  }  document.getElementById('result').innerHTML = tmp;  </script> </body></html>
Salin selepas log masuk

$r = array_fill(0, 12, null);$result = [    ['month'=> '08', 'price'=> 218],    ['month'=> '12', 'price'=> 140],];foreach($result as $v) {  $r[$v['month'] - 1] = $v['price'];}echo json_encode($r);
Salin selepas log masuk
[null,null,null,null,null,null,null,218,null,null,null,140]
Salin selepas log masuk

进一步询问
已知

$res = [[y=>'2014-12-03','item'=>263],[y=>'2014-12-04','item'=>168]];
Salin selepas log masuk

让变成如下格式
[            {y: '2014-12-01', item: null},            {y: '2014-12-02', item: null},            {y: '2014-12-03', item: 263},            {y: '2014-12-04', item: 168},            {y: '2014-12-05', item: null},             .....             {y: '2014-12-31', item1:null},]
Salin selepas log masuk

y加上引?,??才不?有notice

<?php$res = [['y'=>'2014-12-03','item'=>263],['y'=>'2014-12-04','item'=>168]];echo json_encode($res, JSON_PRETTY_PRINT);?>
Salin selepas log masuk


[    {        "y": "2014-12-03",        "item": 263    },    {        "y": "2014-12-04",        "item": 168    }]
Salin selepas log masuk

??

<?php$res = [['y'=>'2014-12-03','item'=>263],['y'=>'2014-12-04','item'=>168]];$tmp = array();foreach($res as $v){	$tmp[$v['y']] = $v['item'];}$start = 1;$end = 31;$result = array();for($i=$start; $i<=$end; $i++){	$key = date('Y-m-d',strtotime('2014-12-'.$i));	$item = null;	if(isset($tmp[$key])){		$item = $tmp[$key];	}	array_push($result, array('y'=>$key,'item'=>$item));}echo json_encode($result, JSON_PRETTY_PRINT);?>
Salin selepas log masuk


[    {        "y": "2014-12-01",        "item": null    },    {        "y": "2014-12-02",        "item": null    },    {        "y": "2014-12-03",        "item": 263    },    {        "y": "2014-12-04",        "item": 168    },    {        "y": "2014-12-05",        "item": null    },    {        "y": "2014-12-06",        "item": null    },    {        "y": "2014-12-07",        "item": null    },    {        "y": "2014-12-08",        "item": null    },    {        "y": "2014-12-09",        "item": null    },    {        "y": "2014-12-10",        "item": null    },    {        "y": "2014-12-11",        "item": null    },    {        "y": "2014-12-12",        "item": null    },    {        "y": "2014-12-13",        "item": null    },    {        "y": "2014-12-14",        "item": null    },    {        "y": "2014-12-15",        "item": null    },    {        "y": "2014-12-16",        "item": null    },    {        "y": "2014-12-17",        "item": null    },    {        "y": "2014-12-18",        "item": null    },    {        "y": "2014-12-19",        "item": null    },    {        "y": "2014-12-20",        "item": null    },    {        "y": "2014-12-21",        "item": null    },    {        "y": "2014-12-22",        "item": null    },    {        "y": "2014-12-23",        "item": null    },    {        "y": "2014-12-24",        "item": null    },    {        "y": "2014-12-25",        "item": null    },    {        "y": "2014-12-26",        "item": null    },    {        "y": "2014-12-27",        "item": null    },    {        "y": "2014-12-28",        "item": null    },    {        "y": "2014-12-29",        "item": null    },    {        "y": "2014-12-30",        "item": null    },    {        "y": "2014-12-31",        "item": null    }]
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan