亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在PHP中從數組中打印多個相同的值?

如何在PHP中從數組中打印多個相同的值?

PHP
叮當貓咪 2022-08-19 16:43:14
我正在嘗試創建一個基本的一致性腳本,該腳本將打印數組內找到的值之前和之后的十個單詞。為此,我將文本拆分為一個數組,標識值的位置,然后打印 -10 和 +10,中間顯示搜索的值。但是,這只是第一次出現這種情況。我知道我可以通過使用array_keys找到其他人(在位置52,78,80中找到),但我不太確定如何循環完成匹配,因為array_keys也會產生數組。因此,使用$matches(帶有array_keys)代替下面的$location不起作用,因為您不能在數組上使用相同的操作數作為整數。有什么建議嗎?謝謝!!<?php$text = <<<EODThe spread of a deadly new virus is accelerating, Chinese President Xi Jinping warned, after holding a special government meeting on the Lunar New Year public holiday.The country is facing a "grave situation" Mr Xi told senior officials.The coronavirus has killed at least 42 people and infected some 1,400 since its discovery in the city of Wuhan.Meanwhile, UK-based researchers have warned of a real possibility that China will not be able to contain the virus.Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will be banned from central districts of Wuhan, the source of the outbreak.EOD;$new = explode(" ", $text);$location = array_search("in", $new, FALSE);$concordance = 10;$top_range = $location + $concordance;$bottom_range = $location - $concordance;while($bottom_range <= $top_range) {    echo $new[$bottom_range] . " ";    $bottom_range++;}?>
查看完整描述

2 回答

?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

您可以只循環訪問array_keys返回的值,使用array_slice提取位置兩側的單詞,然后內爆以再次將句子重新組合在一起:$concordance


$words = explode(' ', $text);

$concordance = 10;

$results = array();

foreach (array_keys($words, 'in') as $idx) {

    $results[] = implode(' ', array_slice($words, max($idx - $concordance, 0), $concordance * 2 + 1));

}

print_r($results);

輸出:


Array

(

    [0] => least 42 people and infected some 1,400 since its discovery in the city of Wuhan.

Meanwhile, UK-based researchers have warned of a

    [1] => not be able to contain the virus.

Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will

    [2] => able to contain the virus.

Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will be banned

)

如果要避免生成類似的短語,其中一個單詞在單詞中出現兩次(例如,上述數組中的索引 1 和 2),則可以在最后一個匹配項的末尾保留一個位置,并跳過該匹配項中出現的事件:$concordance


$words = explode(' ', $text);

$concordance = 10;

$results = array();

$last = 0;

foreach (array_keys($words, 'in') as $idx) {

    if ($idx < $last) continue;

    $results[] = implode(' ', array_slice($words, max($idx - $concordance, 0), $concordance * 2 + 1));

    $last = $idx + $concordance;

}

print_r($results);

輸出


Array

(

    [0] => least 42 people and infected some 1,400 since its discovery in the city of Wuhan.

Meanwhile, UK-based researchers have warned of a

    [1] => not be able to contain the virus.

Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will

)


查看完整回答
反對 回復 2022-08-19
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

試試這個:


<?php


$text = <<<EOD

The spread of a deadly new virus is accelerating, Chinese President Xi Jinping warned, after holding a special government meeting on the Lunar New Year public holiday.

The country is facing a "grave situation" Mr Xi told senior officials.

The coronavirus has killed at least 42 people and infected some 1,400 since its discovery in the city of Wuhan.

Meanwhile, UK-based researchers have warned of a real possibility that China will not be able to contain the virus.

Travel restrictions have come in place in several affected cities. From Sunday, private vehicles will be banned from central districts of Wuhan, the source of the outbreak.

EOD;


$words = explode(" ", $text);

$concordance = 10; // range -+

$result = []; // result array

$index = 0;


if (count($words) === 0) // be sure there is no empty array

    exit;


do {

    $location = array_search("in", $words, false);


    if (!$location) // break loop if $location not found

        break;


    $count = count($words);


    // check range of array indexes

    $minRange = ($location - $concordance > 0) ? ($location-$concordance) : 0; // array can't have index less than 0 (shorthand if)

    $maxRange = (($location + $concordance) < ($count - 1)) ? ($location+$concordance) : $count - 1; // array can't have index equal or higher than array count (shorthand if)


    for ($range = $minRange; $range < $maxRange; $range++) {

        $result[$index][] = $words[$range]; // group words by index

    }


    unset($words[$location]); // delete element which contain "in"

    $words = array_values($words); // reindex array

    $index++;

} while ($location); // repeat until $location exist



print_r($result); // <--- here's your results


?>


查看完整回答
反對 回復 2022-08-19
  • 2 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號