0

我有一个如下所示的数组,在数组键“句点”下,您有一个包含四个数组的数组,您可以在其中看到它打开关闭,打开关闭,我希望它关闭打开,关闭打开,但是每对数组(打开和关闭)仍然需要以父数组格式保留它们的顺序。我真的希望这是有道理的!我一直在努力解决这个问题,但我似乎无法让它正确迭代并保持该顺序。

任何帮助都会非常感激。谢谢你。

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"
        }
      }
    }
  }
}
4

1 回答 1

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)
于 2012-10-13T19:16:35.643 回答