我需要创建一组看起来像这样的数组:
Array ([ID] => 55 [status] => u [resvdate] => 07/16/2018 [price] => 119.00 [source] => C)
Array ([ID] => 56 [status] => u [resvdate] => 07/17/2018 [price] => 119.00 [source] => C)
Array ([ID] => 57 [status] => u [resvdate] => 07/18/2018 [price] => 119.00 [source] => C)
来自五个看起来像这样的数组:
Array ( [resvdate1] => 07/16/2018 [resvdate2] => 07/17/2018 [resvdate3] => 07/18/2018 )
Array ( [resvdateid1] => 55 [resvdateid2] => 56 [resvdateid3] => 57 )
Array ( [resvprice1] => 119.00 [resvprice2] => 119.00 [resvprice3] => 119.00 )
Array ( [pricesource1] => C [pricesource2] => C [pricesource3] => C )
Array ( [rowstatus1] => u [rowstatus2] => u [rowstatus3] => u )
我是否只需要遍历每个数组并选择值,还是有一种更优雅的方法?
答案 0 :(得分:1)
这是一个尝试:
$data = [
[
"resvdate1" => "07/16/2018",
"resvdate2" => "07/17/2018",
"resvdate3" => "07/18/2018"
],
[
"resvdateid1" => 55,
"resvdateid2" => 56,
"resvdateid3" => 57
],
[
"resvprice1" => "119.00",
"resvprice2" => "119.00",
"resvprice3" => "119.00"
],
[
"pricesource1" => "C",
"pricesource2" => "C",
"pricesource3" => "C"
],
[
"rowstatus1" => "u",
"rowstatus2" => "u",
"rowstatus3" => "u"
]
];
$keys = [
"resvdate" => "resvdate",
"resvdateid" => "id",
"resvprice" => "price",
"pricesource" => "source",
"rowstatus" => "status"
];
$result = [];
foreach ($data as $a) {
$i = 0;
foreach ($a as $k => $v) {
$result[$i++][$keys[preg_replace("/\d/", "", $k)]] = $v;
}
}
print_r($result);
Array
(
[0] => Array
(
[resvdate] => 07/16/2018
[id] => 55
[price] => 119.00
[source] => C
[status] => u
)
[1] => Array
(
[resvdate] => 07/17/2018
[id] => 56
[price] => 119.00
[source] => C
[status] => u
)
[2] => Array
(
[resvdate] => 07/18/2018
[id] => 57
[price] => 119.00
[source] => C
[status] => u
)
)
这基本上是列到行的映射,在此过程中需要进行一些小的键调整。
使用repl。