Next () and prev () with php array function
I have the 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, do I want the current position to be "joe", how to show my previous position and my next position?
output:
my prev = *patrick wumbo wambo Peter*
my next = *Glenn Cleveland*
renewal issue
<?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 to see output like
Subject : My brother
Predicate : see
Object : the moon
I'm already trying, but have no idea since last night.
+3
source to share
4 answers
Something like that?
$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:
+1
source to share
Try it...
<?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
0
source to share