0

自由意志で手動で記述されているため、外観で並べ替える必要のある配列があります。値は、表示されると予想されるヒントであることに注意してください。

  $options = array("the array retrieved from some forms");
  $items   = array();
  foreach ($options as $key => $val) {
    switch ($key) {
      case 'three':
        $items[]          = "this should come first";
        $items[]          = "now the second in order";
        $items[]          = "the third";
        $items[]          = "forth";

        switch ($val) {
          case 'a':
          case 'b':
            $items[]       = "fifth";
            $items[]       = "should be six in order";
            break;

          case 'c':
          default:
            $items[]    = "7 in order";
            break;
        }

        break;

..............。

ご覧のとおり、値は何でもかまいませんが、必要なのは、外観に基づいてアイテムを内破して表示することです。そのすべての手動注文、最初に来るものは一番上に印刷する必要があります。

期待される:

"this should come first";
"now the second in order";
"the third";
"forth";
"fifth";
"should be six in order";
"7 in order";

現在の予期しない:

"should be six in order";
"forth";
"7 in order";
"the third";
"fifth";
"this should come first";
"now the second in order";

しかし、私はこのhttp://www.php.net/manual/en/array.sorting.phpからの並べ替えを適用できないようです。 これらの$ itemsは、並べ替える方法がないフォームによってどこかで並べ替えられていると思われます。 。上から下に好きなように出力と順序を書き込む力があります。しかし、自由に並べ替える必要があるという理由だけで、キーを$itemsに挿入することはできません。

出力を確認しましたが、キーが期待どおりに並べ替えられていません。

ヒントは大歓迎です。ありがとう

4

2 に答える 2

2

配列を実行する手順が、希望どおりではないようです。

多分あなたはあなたが望むものを達成するためにいくつかのトリックを使うことができます

たとえば、「キーポインタ」を挿入します

$ikey = 0;
$options = array("the array retrieved from some forms");
  $items   = array();
  foreach ($options as $key => $val) {
    switch ($key) {
      case 'three':
        $items[$ikey++]          = "this should come first";
        $items[$ikey++]          = "now the second in order";
        $items[$ikey++]          = "the third";
        $items[$ikey++]          = "forth";

        switch ($val) {
          case 'a':
          case 'b':
            $items[$ikey++]       = "fifth";
            $items[$ikey++]       = "should be six in order";
            break;

          case 'c':
          default:
            $items[$ikey++]    = "7 in order";
            break;
        }

        break;

不完全なコードを投稿したため、これが役立つかどうかはわかりません。

間違いがあれば英語でごめんなさい

于 2012-04-24T13:12:38.520 に答える
0

phpソースが私たちに見える方法は、コンパイラーと同じではありません。したがって、その方法は不可能です。ただし、ソースで正規表現を実行すると、(良い方法ではありませんが)可能になります。

例:

$source = file_get_contents(__FILE__);
preg_match_all('#\$items\[\]\s*=\s*"([^"]+)"#', $source, $match);
// now $match[1] contains the strings.
$strings = $match[1];
于 2012-04-24T13:09:57.587 に答える