我的代码如下,
<?php
$people = array("patrick", "wumbo", "wambo", "Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>
我的问题是,如果我想当前的位置是“乔”,如何显示我之前的所有位置以及我的所有下一个位置?
输出:
my prev = *patrick wumbo wambo Peter*
my next = *Glenn Cleveland*
<?php
$a = "My brother see the moon";
$b = explode(" ",preg_replace("/(\.|\"|,|;|\(|\)|'|)+?/i","",$a));
for($ulangKata=0;$ulangKata<count($b);$ulangKata++)
{
$huruf_kecil = strtolower($a);
$fungsi_replace = preg_replace("/(\.|\"|,|;|\(|\)|'|)+?/i","",$huruf_kecil);
$pecah_untuk_kata = explode(" ",$fungsi_replace);
$pecah_kata = $pecah_untuk_kata[$ulangKata];
echo "kata ke - ".$ulangKata." ".$b[$ulangKata]."<br>";
}
echo "<br>";
for($ulangKata=0;$ulangKata<count($b);$ulangKata++)
{
echo $b[$ulangKata]."<br>";
}
?>
我希望看到像
这样的输出Subject : My brother
Predicate : see
Object : the moon
我已经尝试过但从昨晚开始就不知道了。
答案 0 :(得分:1)
这样的东西?
TextResource
<强>输出强>:
$current= "Joe";
$people = array("patrick", "wumbo", "wambo", "Peter", "Joe", "Glenn", "Cleveland");
$pos = array_search($current, $people);
foreach($people as $key => $value) {
if($key < $pos) {
$prev[] = $value;
} elseif($key > $pos) {
$next[] = $value;
}
}
foreach($prev as $item) {
echo "$item ";
}
echo "<br />";
foreach($next as $item) {
echo "$item ";
}
更新2
patrick wumbo wambo Peter
Glenn Cleveland
<强>输出:强>
答案 1 :(得分:1)
通过array_slice
轻松实现$key=array_search("Joe",$people);
echo "Current:".$people[$key] . "\n" .
"Prev:".implode(",", array_slice($people, 0, $key)) . "\n" .
"Next:".implode(",", array_slice($people, $key+1));
答案 2 :(得分:0)
试试这个......
<?php
$people = array("patrick", "wumbo", "wambo", "Peter", "Joe", "Glenn", "Cleveland");
$key=array_search("Joe",$people);
$next=array();
$prev=array();
for($i=$key+1;$i<count($people);$i++)
{
$next[]=$people[$i];
}
for($i=$key-1;$i >0;$i--)
{
$prev[]=$people[$i];
}
echo "Current:".$people[$key];
echo "</br>";
echo "Next:".implode(",",$next);
echo "</br>";
echo "prev:".implode(",",$prev);
?>
结果:
Current:Joe
Next:Glenn,Cleveland
prev:Peter,wambo wumbo
答案 3 :(得分:0)
感谢所有人的支持,
如果我有一个像下面这样的字符串
$text = "Brother see the moon";
我签署的代码“看到”为动词,
在“看到”主题
之前的所有数组以及“看到”对象
之后的所有数组