我有一个如下所示的数组,在数组键“句点”下你有一个数组,其中包含四个数组,你可以看到它打开关闭,打开关闭,我希望它关闭打开,关闭打开,但是每对数组(open& close)仍然需要以父数组格式保留它们的顺序。我真的希望这是有道理的!多年来我一直在关注它,但我似乎无法让它正确迭代并保持这个顺序。
非常感谢任何帮助。谢谢。
array(1) {
["opening_hours"]=>
array(1) {
["periods"]=>
array(12) {
[0]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "1"
["time"]=>
string(4) "0730"
}
}
[1]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "1"
["time"]=>
string(4) "1200"
}
}
[2]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "1"
["time"]=>
string(4) "1600"
}
}
[3]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "1"
["time"]=>
string(4) "2345"
}
}
[4]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "2"
["time"]=>
string(4) "0745"
}
}
[5]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "2"
["time"]=>
string(4) "1200"
}
}
[6]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "2"
["time"]=>
string(4) "1600"
}
}
[7]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "2"
["time"]=>
string(4) "2345"
}
}
[8]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "3"
["time"]=>
string(4) "0800"
}
}
[9]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "3"
["time"]=>
string(4) "1200"
}
}
[10]=>
array(1) {
["open"]=>
array(2) {
["day"]=>
string(1) "3"
["time"]=>
string(4) "1600"
}
}
[11]=>
array(1) {
["close"]=>
array(2) {
["day"]=>
string(1) "3"
["time"]=>
string(4) "2345"
}
}
}
}
}
答案 0 :(得分:0)
我想相信你的数组就像这样
$array = array(
"opening_hours" => array(
"periods" => array(
0 => array("open" => array("day" => "1","time" => "0730")),
1 => array("close" => array("day" => "1","time" => "1200")),
2 => array("open" => array("day" => "1","time" => "1600")),
3 => array("close" => array("day" => "1","time" => "2345")),
4 => array("open" => array("day" => "2","time" => "0745")),
5 => array("close" => array("day" => "2","time" => "1200")),
6 => array("open" => array("day" => "2","time" => "1600")),
7 => array("close" => array("day" => "2","time" => "2345")),
8 => array("open" => array("day" => "3","time" => "0800")),
9 => array("close" => array("day" => "3","time" => "1200")),
10 => array("open" => array("day" => "3","time" => "1600")),
11 => array("close" => array("day" => "3","time" => "2345")))));
交换位置
$final = array();
$total = count($array['opening_hours']['periods']);
for($i = 0; $i < $total; $i += 2) {
if (isset($array['opening_hours']['periods'][$i + 1])) {
$final[] = $array['opening_hours']['periods'][$i + 1];
}
$final[] = $array['opening_hours']['periods'][$i];
}
var_dump($final);
输出
array
0 =>
array
'close' =>
array
'day' => string '1' (length=1)
'time' => string '1200' (length=4)
1 =>
array
'open' =>
array
'day' => string '1' (length=1)
'time' => string '0730' (length=4)
2 =>
array
'close' =>
array
'day' => string '1' (length=1)
'time' => string '2345' (length=4)
3 =>
array
'open' =>
array
'day' => string '1' (length=1)
'time' => string '1600' (length=4)
4 =>
array
'close' =>
array
'day' => string '2' (length=1)
'time' => string '1200' (length=4)
5 =>
array
'open' =>
array
'day' => string '2' (length=1)
'time' => string '0745' (length=4)
6 =>
array
'close' =>
array
'day' => string '2' (length=1)
'time' => string '2345' (length=4)
7 =>
array
'open' =>
array
'day' => string '2' (length=1)
'time' => string '1600' (length=4)
8 =>
array
'close' =>
array
'day' => string '3' (length=1)
'time' => string '1200' (length=4)
9 =>
array
'open' =>
array
'day' => string '3' (length=1)
'time' => string '0800' (length=4)
10 =>
array
'close' =>
array
'day' => string '3' (length=1)
'time' => string '2345' (length=4)
11 =>
array
'open' =>
array
'day' => string '3' (length=1)
'time' => string '1600' (length=4)