1

私は Bing とその検索結果に関する研究プロジェクトに取り組んでいます。私はまだ PHP に非常に慣れていないので、少し助けを求めています。昨日、私はすでにここでいくつかの助けを得ました.結果はとても素晴らしかったので、もう一度試しています:)

次のコードがあります。

<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
foreach($r->d->results as $value) {
  foreach($value->Web as $result) {
    echo $result->Title, "\r\n";
    echo $result->Description, "\r\n";
    echo $result->Url, "\r\n";
  }
}
?>

これは基本的に、Bing からの json 結果の 3 つのサイトのタイトル、説明、および URL を示しています (注: 常に 3 つの結果とは限りません。通常は 10 ですが、任意の数にすることができます)。

これが私の大きな使命です。私の研究プロジェクトでは、結果をランダムにシャッフルする必要があります。つまり、たとえば次のように表示したいと考えています。

初めてページをロードします: CakePHP: 迅速な開発の php フレームワーク。Pages PHP Presents PHP: ハイパーテキスト プリプロセッサ

2 回目にページをロードします。ページ

3 回目にページをロードします。 PHP: ハイパーテキスト プリプロセッサ CakePHP: 迅速な開発の php フレームワーク。ページ PHP プレゼント

繰り返しますが、上記の順序は単なる例です。特定の順序なしでランダムに実行したいのです。

以前のヘルパーに感謝したいと思います。あなたの答えを楽しみにしています。

これを読んでくれてありがとう:)

更新: 誰かがとても親切で、最初の 3 つを除いて結果のみをシャッフルする例を見せてくれたら、それは本当に素晴らしいことです.

4

2 に答える 2

2

一時配列を使用して行を格納し、shuffleメソッドを使用してそれをランダム化できます。

<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
$tmpArray = array();
foreach($r->d->results as $value) {
  foreach($value->Web as $result) {
    $string = $result->Title."\r\n";
    $string.= $result->Description."\r\n";
    $string.= $result->Url."\r\n";
    $tmpArray[] = $string;
  }
}
shuffle($tmpArray);
foreach($tmpArray as $string){
    echo $string;
}
?>

これもうまくいくかもしれません:

$r = json_decode($r);
$r->d->results = shuffle($r->d->results);

編集:最初の3つを除くすべてのキーをシャッフルするには:

<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
$tmpArray = array();
foreach($r->d->results as $value) {
  foreach($value->Web as $result) {
    $string = $result->Title."\r\n";
    $string.= $result->Description."\r\n";
    $string.= $result->Url."\r\n";
    $tmpArray[] = $string;
  }
}
$limit = min(3,count($tmpArray)) -1;
for($i=0;$i<$limit;$i++){
    echo array_shift($tmpArray); // echo and remove first item
}
shuffle($tmpArray);
foreach($tmpArray as $string){
    echo $string;
}
?>
于 2012-10-02T12:06:38.123 に答える
0
<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = shuffle(json_decode($r));
foreach($r->d->results as $value) {
  foreach($value->Web as $result) {
    echo $result->Title, "\r\n";
    echo $result->Description, "\r\n";
    echo $result->Url, "\r\n";
  }
}
?>
于 2012-10-02T12:08:19.063 に答える