2012-10-02 4 views
1

Bing과 관련한 조사 프로젝트에서 작업 중이며 검색 결과가 있습니다. 아직 PHP에 익숙하지 않아 조금만 도움이됩니다. 어제 나는 이미 여기에 몇 가지 도움말을 가지고 그 결과는 내가 :) 한 번 더 시도하고 너무 좋았어요 PHP JSON - JSON 파일에 액세스하고 값을 표시하기 전에 임의로 변경하십시오.

나는 다음과 같은 코드가 있습니다

<?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"; 
    } 
} 
?> 

이 기본적으로 세 가지의 제목, 설명 및 URL을 보여줍니다 Bing의 json 결과 사이트 (참고 : 항상 3 가지 결과가있는 것은 아니며 일반적으로 10이지만 임의의 숫자 일 수 있음)

이제 내 큰 임무는 다음과 같습니다. 내 연구 프로젝트의 경우, 결과는 무작위로, 예를 들어 다음과 같이보고 싶습니다.

처음으로 페이지를로드합니다 : CakePHP : 신속한 개발 PHP 프레임 워크. 내가 페이지를로드 하이퍼 텍스트 전 처리기

두 번째 시간 : 페이지 PHP는 PHP를 선물 하이퍼 텍스트 전 처리기 CakePHP의 : 급속한 개발 PHP 프레임 워크 PHP는 PHP를 표시합니다. 페이지

세 번째로 페이지를로드합니다 : PHP : 하이퍼 텍스트 프리 프로세서 CakePHP : 신속한 개발 PHP 프레임 워크. 페이지 PHP 선물

다시 위의 순서는 단지 예일뿐입니다. 특정 순서없이 무작위로 원합니다.

이전 헬퍼에게 감사하고 당신의 답변을 기다리고 있습니다. 사람이 너무 친절 될 것 경우도 나에게 첫 번째 세 제외한 결과를 셔플의 예를 보여 - 정말 좋은 것 :

당신의 시간이 :)

업데이트를 읽어 주셔서 감사합니다.

+0

편집 대답은 ... –

답변

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); 

편집 : 모든 키하지만 처음 세를 섞으려면 :

<?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; 
} 
?> 
+0

안녕하세요 해결책을 가져 주셔서 감사합니다. 한 가지 : 코드의 1 부분에 경고가 표시됩니다. 14 번째 줄에있는 /home/user/domains/domain.com/public_html/test.php의 foreach()에 잘못된 인수가 제공되었습니다. –

+0

어쩌면이 코드를보실 수 있겠습니까? http://codepad.org/ubYt3zZH - 사용 해봤습니다 (첫 번째 3 개의 결과를 유지하고 나머지는 임의로 뒤집는 두 번째 부분입니다).하지만 어떤 이유로이 작업이 실패합니다. 편집 한 코드의 첫 번째 부분은 잘 작동합니다. –

+1

json 문자열에 문제가 있습니다. 구문을 확인하십시오. 그리고 대답을 받아들이십시오 ;-) –

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"; 
    } 
} 
?> 
세 첫번째 결과