このURLのコンテンツを取得するにはfile_get_contents()を使用しました
http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc
配列が返されるようです。 。しかし、foreach ループをどのように使用しても、エラーが発生します。 。
配列内の単語の値を取り出したいだけです。 。誰が私を助けてくれますか、緊急です
得られたものは文字列です。したがって、foreach は絶対に必要ありません
通常のルールまたは他のメソッドを使用して単語の値を取得します。
$s = file_get_contents('http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc');preg_match_all('/\[word\] => (.+)/', $s, $m);print_r($m[1]);
Array( [0] => 1314 [1] => abc)
$s=file_get_contents('http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc');$rule='#(?<=\[word\] =>)\s\w+#';preg_match_all($rule,$s,$arr);print_r($arr);
Array( [0] => Array ( [0] => 1314 [1] => abc ))
file_get_contents() は、ページを取得するときに文字列を返します。
リモート インターフェイス経由で呼び出す必要がある場合は、http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc の出力を json 形式の文字列に変更できることをお勧めします。
呼び出しサイトでコンテンツを取得したら、json_decode を使用してデコードし、php 配列を取得します。
さらに、リモート Web ページのコンテンツを取得するために file_get_contents を使用することは推奨されません。curl を使用することをお勧めします。
http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc
返されるものは次のとおりです:
string(247) "Array ( [0] => Array ( [word] => 1314 [単語タグ] => 90 [インデックス] => 0 ) [1] => 配列 ( [単語] => abc [単語タグ] => 95 [インデックス] => 1 ) ) / /配列ではなく配列構造の文字列
//Encoding
$arr = array( 0=>array( 'word '=> 1314, 'word_tag'=> 90, 'index' => 0 ), 1 => Array( 'word' => 'abc', 'word_tag' => 95, 'index' => 1 ));echo( json_encode($arr) );
$arr = array();$url = 'http://simonfenci.sinaapp.com/index.php?key=simon&wd=1314abc';$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 0);$output = curl_exec($ch);$arr = json_decode($output,true);curl_close($ch);
//json によって返される文字列
[{"word ":1314,"word_tag":90,"index":0},{"word":"abc","word_tag ":95,"index":1}]
//serialize によって返される文字列
a:2:{i:0;a:3:{s:5:"word ";i:1314;s:8 :"単語タグ";i:90;s:5:"インデックス";i:0;}i:1;a:3:{s:4:"単語";s:3:"abc";s:8 :"word_tag";i:95;s:5:"index";i:1;}}
は直接の var_export($val,true); の出力より明らかに短く、簡単に復元できます。
皆さんありがとうございます、ついに完成しました