如果您想创建自定义函数,请将数组传递给函数。在这个函数中,你可以按照你想要的方式过滤数据。我创建了formatRow()函数,它只需从数组中删除最后需要的键,然后再次插入它,因为新值总是插入到数组的末尾,所以可以得到所需的输出
$row = array(
'one'=>"data",
'four'=>"data",
'end'=>"enddata",
'three'=>"data",
'two'=>"data",
);
$formatted_row = formatRow($row,'end');
echo "<pre>";
var_dump($formatted_row);
//output
// array(5) {
// ["one"]=>
// string(4) "data"
// ["four"]=>
// string(4) "data"
// ["three"]=>
// string(4) "data"
// ["two"]=>
// string(4) "data"
// ["end"]=>
// string(7) "enddata"
// }
function formatRow ($row,$key_that_you_need_last) {
if (array_key_exists($key_that_you_need_last,$row)) {
$value = $row["$key_that_you_need_last"];
unset($row["$key_that_you_need_last"]);
$row["$key_that_you_need_last"] = $value;
}
return $row;
}
如果你想取得更多成就,以下是你可能想要通过的链接。
array_map
array_walk