Reputation: 29
I have a code below,
<?php
$people = array("patrick", "wumbo", "wambo", "Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>";
echo next($people) . "<br>";
echo prev($people);
?>
my question is if I want current position is "joe", how to show my all previous position and my all next position ?
output:
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>";
}
?>
and I want see the output like
Subject : My brother
Predicate : see
Object : the moon
I have already trying but have no idea since last night.
Upvotes: 3
Views: 254
Reputation: 1816
Something like this?
$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 ";
}
Output:
patrick wumbo wambo Peter
Glenn Cleveland
UPDATE 2
<?php
function getSubObj($string, $verb) {
$parts = explode(" ", $string);
$pos = array_search($verb, $parts);
foreach($parts as $key => $value) {
if($key < $pos) {
$subjects[] = $value;
} elseif($key > $pos) {
$objects[] = $value;
}
}
return array($subjects, $objects);
}
list($subjects, $objects) = getSubObj("Brother see the moon", "see");
echo "<h2>Subjects</h2>";
foreach($subjects as $item) {
echo "$item ";
}
echo "<h2>Objects</h2>";
foreach($objects as $item) {
echo "$item ";
}
?>
Output:
Upvotes: 1
Reputation: 29
Thank's all for your support,
Hmm if I have a String like below
$text = "Brother see the moon";
I sign to my code that "see" as verb,
all array before "see" as subject
and all array after "see" as object
Upvotes: 0
Reputation: 26153
make it easy by 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));
Upvotes: 1
Reputation: 5444
Try this...
<?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);
?>
Result:
Current:Joe
Next:Glenn,Cleveland
prev:Peter,wambo wumbo
Upvotes: 0