为了账号安全,请及时绑定邮箱和手机立即绑定

如何在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 关注
  • 120 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号