1

補充中の製品 (replenish_departments) と再注文できない製品 (fashion_departments) などの在庫情報を格納する基本的な zend_config_xml インスタンスがあります。(参考までに、当社の製品は部門に分類されており、各部門には固有のアルファ コードがあります) 私の xml は次のようになります。

<inventory>
 <settings>
  <allow_backorders>1</allow_backorders>
  <replenish_departments>
   <department>M</department>
  </replenish_departments>
  <fashion_departments>
   <department>MF</department>
   <department>MS</department>
  </fashion_departments>
 </settings>
</inventory>

私ができるようにする必要があるのは、特定の部門コードが補充中か流行中かをすばやく判断することです。私がしようとしていたことは単純でした(またはそう思った):

foreach ($inv_settings->replenish_departments as $replenish_deptcode) {
 if ($given_deptcode == $replenish_deptcode) return true;
}

しかし、私が発見したのは、単一の子ノードがある場合、それを反復処理できないということです。つまり、このコードは fashion_departments を意味しますが、replenish_departments は意味しません。

ここでのトリックは何ですか?

編集: $inv_settings を foreach 内の配列として型キャストすると、エラーなしで反復できることがわかりました。今のところ、これは私が使用している方法ですが、私はまだより良い修正を受け入れています.

4

3 に答える 3

0

ここにたどり着く他の人々(私のように)のためだけに。

これは最新バージョンの zend フレームワークで実現できることを共有したいと思います。1.11.11 を使用しましたが、しばらくして修正されましたhttp://framework.zend.com/issues/browse/ZF-2285を参照してください

    $xml = '<?xml version="1.0"?>
    <inventory>
        <settings>
    <allow_backorders>1</allow_backorders>
   <replenish_departments>
      <department>M</department>
   </replenish_departments>
   <fashion_departments>
      <department>MF</department>
      <department>MS</department>
   </fashion_departments>
   </settings>
   </inventory>';

    $article = new Zend_Config_Xml($xml);
Zend_Debug::dump($article->toArray());

戻り値

array(1) {
  ["settings"] => array(3) {
    ["allow_backorders"] => string(1) "1"
      ["replenish_departments"] => array(1) {
         ["department"] => string(1) "M"
      }
      ["fashion_departments"] => array(1) {
        ["department"] => array(2) {
         [0] => string(2) "MF"
         [1] => string(2) "MS"
      }
    }
  }
}

ルートの複数の要素を許可していないようです。

   <inventory>
     value1
   </inventory>
   <inventory>
     value2
   </inventory>
于 2012-05-15T15:46:59.080 に答える
0

私はこれを簡単に書きました。

$xml = simplexml_load_string("<inventory>
 <settings>
  <allow_backorders>1</allow_backorders>
  <replenish_departments>
   <department>M</department>
  </replenish_departments>
  <fashion_departments>
   <department>MF</department>
   <department>MS</department>
  </fashion_departments>
 </settings>
</inventory>
");

foreach ($xml->settings->replenish_departments as $replenish_departments) {
    foreach ($replenish_departments as $department)
    {
         if ($given_deptcode == $department) 
            return true;
    }   
}
于 2010-08-27T08:35:12.623 に答える
0

サンプルの XML 構成ファイルとコードは、私にとっては問題なく動作するようです。私が使用したスニペットは次のとおりです。

$given_deptcode = 'M';
$configuration  = new Zend_Config_Xml($config_file);
$inv_settings   = $configuration->settings;
foreach ($inv_settings->replenish_departments as $replenish_deptcode) {
    if ($replenish_deptcode == $given_deptcode) {
        echo $replenish_deptcode . ' needs replenishing!' . PHP_EOL;
    }
}

これにより、期待される出力が得られます。

Mは補充が必要です!

1 つのアイテムを繰り返し処理できないという結論にどのように到達したのかわかりません。

PS配列に型キャストするのではなく、toArray()メソッドを使用して構成(またはその一部)を配列形式で取得できます。

于 2010-08-27T20:31:35.730 に答える