$sort = ['advertiser_id','advertiser_name','raw','valid_clicks_count','invalid_clicks_count','conversions_count','revenue'];
$arr = Array(
[0] => Array
(
[advertiser_id] => 1
[valid_clicks_count] => 4
[conversions_count] => 2
[revenue] => 0.40
[invalid_clicks_count] => 3
[raw] => 7
[advertiser_name] => Ad1
)
)
以上二维数组中的一维数组的键根据$sort
的值进行排序
现有两种方式:
方式一:
创建一个新的数组,循环赋值,再注销掉原来的数组
$newArr = [];
foreach ($arr as $key => $val){
$newArr[$key]['advertiser_id'] = $val['advertiser_id'];
$newArr[$key]['advertiser_name'] = $val['advertiser_name'];
$newArr[$key]['raw'] = $val['raw'];
$newArr[$key]['valid_clicks_count'] = $val['valid_clicks_count'];
$newArr[$key]['invalid_clicks_count'] = $val['invalid_clicks_count'];
$newArr[$key]['conversions_count'] = $val['conversions_count'];
$newArr[$key]['revenue'] = $val['revenue'];
}
unset($exportData);
效果符合期望:
Array
(
[0] => Array
(
[advertiser_id] => 1
[advertiser_name] => Ad1
[raw] => 7
[valid_clicks_count] => 4
[invalid_clicks_count] => 3
[conversions_count] => 2
[revenue] => 0.40
)
)
测试运行时间和空间的数据
开始内存:377704Array
(
[0] => Array
(
[advertiser_id] => 1
[advertiser_name] => Ad1
[raw] => 7
[valid_clicks_count] => 4
[invalid_clicks_count] => 3
[conversions_count] => 2
[revenue] => 0.40
)
)
<br />[页面执行时间:3.9815902709961E-5 ]秒<br />运行后内存:378800回到正常内存:378800
方式二:适用php的内置函数 array_flip,array_replace
$sort = ['advertiser_id','advertiser_name','raw','valid_clicks_count','invalid_clicks_count','conversions_count','revenue'];
foreach ($data as &$val){
$val = (array_replace(array_flip($sort), $val));
}
效果符合期望值
开始内存:376088Array
(
[0] => Array
(
[advertiser_id] => 1
[advertiser_name] => Ad1
[raw] => 7
[valid_clicks_count] => 4
[invalid_clicks_count] => 3
[conversions_count] => 2
[revenue] => 0.40
)
)
<br />[页面执行时间:2.1934509277344E-5 ]秒<br />运行后内存:376728回到正常内存:376728
经过多次测试,两者运行平均时间相差不大,可忽略,运行内存消耗方式一大于方式二,推荐用方式二来处理。
PS:测试脚本
<?php
echo '开始内存:'.memory_get_usage(), '';
$stime=microtime(true); //获取程序开始执行的时间
$data = '[{"advertiser_id":1,"valid_clicks_count":4,"conversions_count":2,"revenue":"0.40","invalid_clicks_count":3,"raw":7,"advertiser_name":"Ad1"}]';
$data = json_decode($data, true);
//方式一:
$sortExportData = [];
foreach ($data as $key => $val){
$sortExportData[$key]['advertiser_id'] = $val['advertiser_id'];
$sortExportData[$key]['advertiser_name'] = $val['advertiser_name'];
$sortExportData[$key]['raw'] = $val['raw'];
$sortExportData[$key]['valid_clicks_count'] = $val['valid_clicks_count'];
$sortExportData[$key]['invalid_clicks_count'] = $val['invalid_clicks_count'];
$sortExportData[$key]['conversions_count'] = $val['conversions_count'];
$sortExportData[$key]['revenue'] = $val['revenue'];
}
print_r($sortExportData);
unset($data);
//方式二:
$sort = ['advertiser_id','advertiser_name','raw','valid_clicks_count','invalid_clicks_count','conversions_count','revenue'];
foreach ($data as &$val){
$val = (array_replace(array_flip($sort), $val));
}
print_R($data);
$etime=microtime(true);//获取程序执行结束的时间
$total=$etime-$stime; //计算差值
echo "<br />[页面执行时间:{$total} ]秒".'<br />';
echo '运行后内存:'.memory_get_usage(), '';
unset($tmp);
echo '回到正常内存:'.memory_get_usage();
网友评论