2014-02-24 31 views
0

我有一個數組:片陣列在選定點之前和之後選擇一個包容

$input = array("a", "b", "c", "d", "e"); 

用戶可以選擇一個字母,然後我要分配他們選擇什麼,一個值兩側變量。

E.g.用戶選擇「C」產生:

$userVariable = 'c'; 

$input = array("a", "b", "c", "d", "e"); 

// do something to $input and produce 

$letter1 = 'b'; 
$letter2 = 'c'; 
$letter3 = 'd'; 

我猜array_slice(),但是從它讀了,我不能找到一種方法,可以在之前和之後選擇值確實似乎「切片」在一個數值,而不是到實際的價值。

而且,如果「一」選擇然後我需要返回a,b,c如果「E」選擇我要回e,d,c

這可能嗎?

回答

0
<?php 
$userVariable = 'e'; 
$input = array("a", "b", "c", "d", "e");  

// get position of $userVar in $input 
$pos = array_search($userVariable,$input); 
if($pos == 0){ 
    // the first one 
    $letter1 = $input[$pos]; 
    $letter2 = $input[$pos+1]; 
    $letter3 = $input[$pos+2]; 
}elseif($pos==count($input)-1){ 
    // the last one 
    $letter1 = $input[$pos-2]; 
    $letter2 = $input[$pos-1]; 
    $letter3 = $input[$pos]; 
}else{ 
    // others 
    $letter1 = $input[$pos-1]; 
    $letter2 = $input[$pos]; 
    $letter3 = $input[$pos+1]; 
} 
var_dump($letter1,$letter2,$letter3); 

?> 
+0

完美複製BTW! –

+0

@AncientGeek,這不是一個副本,你的回答'==='更好!我已經投票了你的答案。 – TopCaver

2
<?php 

$input = array("a", "b", "c", "d", "e"); 
$find = 'c'; 
$key = array_search($find, $input); //get the key index of user input 
echo $key; 
echo '<br />'; 
$total = count($input); 

if ($key === 0) 
{ 
    echo $input[$key]; 
    echo $input[$key+1]; 
    echo $input[$key+2]; 
} 
else if($key === ($total-1)) 
{ 
    echo $input[$key-2]; 
    echo $input[$key-1]; 
    echo $input[$key]; 
} 
else 
{ 
    echo $input[$key-1]; 
    echo $input[$key]; 
    echo $input[$key+1]; 
} 

?>

0
$input = array("a", "b", "c", "d", "e"); 
$userChoice="c"; 

for ($i=0; $i<count($input); $i++) 
{ 
    if ($input[$i]==$userChoice) 
    { 
     $letter1=$input[$i-1]; 
     $letter2=$input[$i]; 
     $letter3=$input[$i+1]; 
    } 
} 

echo "Letter 1: ".$letter1."<br />"; 
echo "Letter 2: ".$letter2."<br />"; 
echo "Letter 3: ".$letter3; 
+1

注意,這會給出一個數組如果用戶選擇「a」或「e」,則索引超出界限。我會讓你像你一樣處理。 –

0

$key = array_search('c', $input); 

那麼數組值,他們關鍵的首先獲得指數

if(isset($input[$key-1])) 
$letter1 = $input[$key-1]; 

$letter2 = $input[$key]; 
if(isset($input[$key+1])) 
$letter3 = $input[$key+1]; 
0
<?php 

    $userVariable = 'c'; 
    $input = array("a", "b", "c", "d", "e"); 

    foreach ($input as $idx => $value) { 
     if ($userVariable == $value) { 
      if ($idx == 0) { 
       print $input[$idx] . "," .$input[$idx+1] . "," .$input[$idx+1]; 
      } else { 
       if ($idx == count($input) - 1) { 
        print $input[$idx-2] . "," .$input[$idx-1] . "," .$input[$idx]; 
       } else { 
        print $input[$idx-1] . "," .$input[$idx] . "," .$input[$idx+1]; 
       } 
      } 
     } 
    } 
?> 
0

試下面的代碼:

$input = array("a", "b", "c", "d", "e"); 
$key = array_search('a', $input); 

echo $prev = (isset($input[$key - 1]))?$input[$key - 1]:""; 
echo $selected = $input[$key]; 
echo $next = (isset($input[$key + 1]))?$input[$key + 1]:""; 
0

使用array_slice():

$input = array("a", "b", "c", "d", "e"); 
$choice = 'e'; 

$result = array_slice(
    $input, 
    ((array_search($choice, $input) - 1) > 0) ? array_search($choice, $input) - 1 : 0, 
    3 
); 
var_dump($result); 

,雖然它並沒有給e,d,c如果選擇e,只是d,e

相關問題