0

以下のような配列があります。配列キー「ピリオド」の下に、4つの配列を含む配列があります。この配列では、開いて閉じて、開いて閉じて、閉じて開いて、閉じて開きますが、配列の各ペア(オープンとクローズ)は、親配列形式で順序を保持する必要があります。私はそれが理にかなっていることを本当に望んでいます!私は何年もの間これをプラグインしてきましたが、正しく反復してその順序を維持することができないようです。

どんな助けでも本当にありがたいことに受けられるでしょう。ありがとうございました。

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 に答える