2

以下は、アレイの構造のサンプルです。[245][subfields][a]などの特定の値を引き出すことができる必要があります。各配列内には、可変数の[フィールド]と可変数の[サブフィールド]があります。提案をありがとう!

編集:取得しているネイティブJSON形式で配列を含めました。次に、json_decodeを使用して、これをphpで使用可能な配列に変換していました。より良い提案があれば、私に知らせてください。

きれいな配列へのリンク

{"leader":"01220nam  2200265   4500","fields":[{"001":"ocm00000197"},{"005":"19880526181046.0"},{"008":"690325r19681924nyua          00000 eng  "},{"010":{"ind1":" ","ind2":" ","subfields":[{"a":"67-020193 \/\/r832"}]}},{"035":{"ind1":" ","ind2":" ","subfields":[{"a":".b10000021"},{"b":"a    "},{"c":"-"}]}},{"041":{"ind1":"1","ind2":" ","subfields":[{"a":"eng"},{"a":"fre"}]}},{"049":{"ind1":" ","ind2":" ","subfields":[{"a":"JBLA"},{"c":"$8.00"}]}},{"100":{"ind1":"1","ind2":"0","subfields":[{"a":"Allemagne, Henry Ren\u00e2e d',"},{"d":"1863-1950."}]}},{"245":{"ind1":"1","ind2":"0","subfields":[{"a":"Decorative antique ironwork :"},{"b":"a pictorial treasury \/"},{"c":"by Henry Ren\u00e2e d'Allemagne ; introd., bibliography, and translation of captions by Vera K. Ostoia."}]}},{"260":{"ind1":"0","ind2":" ","subfields":[{"a":"New York :"},{"b":"Dover Publications,"},{"c":"[1968]"}]}},{"300":{"ind1":" ","ind2":" ","subfields":[{"a":"x, 415 p. :"},{"b":"(chiefly ill.) ;"},{"c":"31 cm."}]}},{"500":{"ind1":" ","ind2":" ","subfields":[{"a":"\"This Dover edition ... is a republication of all the plates in the two portfolios of Mus\u00e2ee Le Secq des Tournelles \u00e1a Rouen: Ferronnerie ancienne, originally published ... in 1924.\""}]}},{"500":{"ind1":" ","ind2":" ","subfields":[{"a":"\"Other books by Henry Ren\u00e2e d'Allemagne\": p. x."}]}},{"590":{"ind1":" ","ind2":" ","subfields":[{"a":"1373336."}]}},{"650":{"ind1":" ","ind2":"0","subfields":[{"a":"Art metal-work."}]}},{"700":{"ind1":"1","ind2":"0","subfields":[{"a":"Ostoia, Vera K."}]}},{"710":{"ind1":"2","ind2":"0","subfields":[{"a":"Mus\u00e2ee Le Secq des Tournelles."}]}},{"830":{"ind1":" ","ind2":"0","subfields":[{"a":"Dover pictorial archive series."}]}},{"999":{"ind1":" ","ind2":" ","subfields":[{"b":"1"},{"c":"901102"},{"d":"m"},{"e":"b"},{"f":"-"},{"g":"0"}]}},{"945":{"ind1":" ","ind2":" ","subfields":[{"a":"739.4\/ALLEMAGNE,H"},{"g":"1"},{"i":"31184001373336"},{"l":"abx7 "},{"n":"moved from RESEARCH collection - Feb 2012"},{"o":"-"},{"p":"$8.00"},{"s":"-"},{"t":"235"},{"u":"10"},{"v":"0"},{"w":"0"},{"x":"1"},{"y":".i10000021"},{"z":"901102"}]}}]}
4

4 に答える 4

1
// behaves like  array(*, 245, *, 'subfields', *, 'a', *);
// where * is a wildcard for 0 or more keys
$keySequence = array(245, 'subfields', 'a');


$arr = array();
$arr[245]['subfields']['a'] = 'i match';
$arr[245]['subfields'][7]['a'] = 'i match';
$arr[0][0][0][245][0][0]['subfields']['subfields'][0]['a']['a'] = 'i match';
$arr[0][0][0][0][0][0]['subfields']['subfields'][0]['a']['a'] = 'i dont match';
$arr[0][0][0][0][0][0]['subfields']['subfields'][0]['a']['a'] = 'i dont match';





$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr));
$filtered = new CallbackFilterIterator($iter, function ($_, $_, $iterator) use ($keySequence) {
    $i = 0;
    $max = count($keySequence);
    foreach (range(0, $iterator->getDepth()) as $depth) {
        if ($keySequence[$i] === $iterator->getSubIterator($depth)->key())
            $i++;
        if ($i === $max)
            return true;
    }
    return $i === $max;
});

foreach ($filtered as $leafVal) {
    echo "$leafVal\n";
}

http://codepad.viper-7.com/9t4qVO

于 2012-12-11T22:09:50.643 に答える
1

fieldsそのため、キーをループして、各サブ配列の最初のキーをチェックして、検索したアイテムと一致するメソッドが必要になるようです。

// Loop over the fields, which are each arrays numerically indexed...
foreach ($record['fields'] as $key => $field) {
  // Get the field's first array key, which is 010, 015, etc...
  $marc_keys = array_keys($field);
  // the first key...
  $marc_field = $marc_keys[0];
  // Note in PHP 5.4 you could dereference it more easily like
  // $marc_field = array_keys($field)[0];

  // Test if the key you found is the one you want...
  if ($marc_field == '245') {
     foreach ($field[$marc_field]['subfields'] as $subkey => $subfield) {
       // Get the subfield identifier (the first array key)
       $sf_keys = array_keys($subfield);
       $sf_identifier = $sf_keys[0];
       if ($sf_identifier == 'a') {
          // Congratulations Arthur, you found the grail....
          // Do something with it...
       } 
     }
  }
  // Otherwise, keep looping until you find the one you need...
}

関数として:

$field,$subfieldこれをパラメーターとして受け入れる関数に変換することをお勧めします。

同じタイプの複数のサブフィールドを考慮して更新し、配列を返します。

function getSubfield($record, $in_field, $in_subfield) {
    // Array to hold output for multiple like subfields
    $subfields_returned = array();

    foreach ($record['fields'] as $key => $field) {
      $marc_keys = array_keys($field);
      $marc_field = $marc_keys[0];

      // Test if the key you found is the one you want (which is the $in_field param)
      if ($marc_field == $in_field) {
         foreach ($field[$marc_field]['subfields'] as $subkey => $subfield) {

           $sf_keys = array_keys($subfield);
           $sf_identifier = $sf_keys[0];

           // And match the subfield to the function param $in_subfield

           if ($sf_identifier == $in_subfield) {
              // Congratulations Arthur, you found the grail....
              // Return it...
              $subfields_returned[] = $subfield[$sf_identifier];
           }
         }
          // After the subfield loop, you can return the accumulated array
         if (count($subfields_returned) > 0) return $subfields_returned;
      }
      // Otherwise, keep looping until you find the one you need...
    }
}

ここにデモンストレーションがあります

...部分的に組み上げたレコードで...

于 2012-12-11T21:12:48.210 に答える
0

このデータに対して多くのカスタム検索を行う場合は、既に作成されているさまざまな関数のいずれかを使用して、配列を XML に変換することを検討できます。次に、XPath を使用してデータを柔軟にクエリできます。

于 2012-12-11T21:16:51.453 に答える
0
function recursiveArrayIterator($arr){
    foreach($arr as $key => $arr2){
        if(is_array($arr2)){
           recursiveArrayIterator($arr2);
        }
    }
}
$arr = array(array('key'=>'value'));
recursiveArrayIterator($arr);

私はあなたの要件ではありませんが、これはあなたを助けるかもしれません.

于 2012-12-11T21:19:05.383 に答える