2011-02-23 17 views
35

में अगला तत्व प्राप्त करें मेरे पास फ़ोरैच लूप है और मैं देखना चाहता हूं कि लूप में अगला तत्व है या नहीं, इसलिए मैं वर्तमान तत्व की तुलना अगले के साथ कर सकता हूं। मैं यह कैसे कर सकता हूँ? मैंने वर्तमान और अगले कार्यों के बारे में पढ़ा है लेकिन मुझे पता नहीं लगा कि उनका उपयोग कैसे किया जाए।फोरैच लूप

अग्रिम धन्यवाद

+0

क्या यह तत्व # 2 से शुरू करने के लिए स्वीकार्य होगा और इसे वापस तत्व # 1 से तुलना करने के लिए स्वीकार्य होगा? –

+0

नहीं इसे 1 – chchrist

उत्तर

24

एक अनूठा तरीका सरणी और तो पाश उल्टा करने के लिए किया जाएगा। यह रूप में अच्छी तरह से गैर संख्यानुसार अनुक्रमित सरणियों के लिए काम करेंगे: आप अभी भी current और next कार्यों का उपयोग करने में रुचि रखते हैं

$items = array(
    'one' => 'two', 
    'two' => 'two', 
    'three' => 'three' 
); 
$backwards = array_reverse($items); 
$last_item = NULL; 

foreach ($backwards as $current_item) { 
    if ($last_item === $current_item) { 
     // they match 
    } 
    $last_item = $current_item; 
} 

, तो आप ऐसा कर सकते हैं:

$items = array('two', 'two', 'three'); 
$length = count($items); 
for($i = 0; $i < $length - 1; ++$i) { 
    if (current($items) === next($items)) { 
     // they match 
    } 
} 

# 2 शायद सबसे अच्छा है उपाय। नोट, $i < $length - 1; सरणी में पिछले दो आइटम की तुलना करने के बाद लूप को रोक देगा। मैंने इसे लूप में उदाहरण के साथ स्पष्ट करने के लिए रखा है। ,

//$arr is the array you wish to cycle through 
$keys = array_keys($arr); 
$num_keys = count($keys); 
$i = 1; 
foreach ($arr as $a) 
{ 
    if ($i < $num_keys && $arr[$keys[$i]] == $a) 
    { 
     // we have a match 
    } 
    $i++; 
} 

यह दोनों सरल सरणियों के लिए काम करेंगे: आप शायद सिर्फ $length = count($items) - 1;

6

तो अनुक्रमित continious एआर:

foreach($arr as $key => $val){ 
    if(isset($arr[$key+1])){ 
     echo $arr[$key+1]; // next element 
    }else{ 
    //end of array reached 
    } 
} 
+1

से शुरू होना चाहिए यह सच नहीं है, कोशिश करें: सरणी (1 => 'ए', 0 => 'बी', 100 => 'सी'); –

+7

@EduardoRomero yep, यही कारण है कि मैंने उल्लेख किया: 'अगर सूचकांक निरंतर है' –

4

अगर इसकी संख्यानुसार अनुक्रमित:

foreach ($foo as $key=>$var){ 

    if($var==$foo[$key+1]){ 
     echo 'current and next var are the same'; 
    } 
} 
1

आप foreach से पहले सरणी की चाबी मिल सकता है, तो अगले तत्व की जांच करने के लिए एक काउंटर का उपयोग गणना करना चाहिए, की तरह कुछ जैसे array(1,2,3), और array('first'=>1, 'second'=>2, 'thrid'=>3) जैसे कुंजी वाले सरणी।

8

php.net/foreach के रूप में बताते हैं:

जब तक सरणी संदर्भित है, foreach निर्दिष्ट सरणी की एक प्रति और न सरणी पर ही चल रही है। foreach सरणी सूचक पर कुछ दुष्प्रभाव है। रीसेट किए बिना फोरैच के दौरान या बाद में सरणी सूचक पर भरोसा न करें।

दूसरे शब्दों में - यह करना बहुत अच्छा विचार नहीं है कि आप क्या करना चाहते हैं। शायद किसी के साथ बात करने का अच्छा विचार होगा कि आप ऐसा करने का प्रयास क्यों कर रहे हैं, देखें कि कोई बेहतर समाधान है या नहीं? अगर आपके पास कोई अन्य संसाधन उपलब्ध नहीं है तो irc.freenode.net पर ## PHP में हमसे पूछने के लिए स्वतंत्र महसूस करें।

9

आप शायद इस्तेमाल कर सकते हैं, जबकि foreach के बजाय पाश:

while ($current = current($array)) 
{ 
    $next = next($array); 
    if (false !== $next && $next == $current) 
    { 
     //do something with $current 
    } 
} 
+0

मुझे यह सहायक पाया ... धन्यवाद! –

+0

बस याद रखें, आपको इससे पहले 'रीसेट ($ सरणी)' को रीसेट करना पड़ सकता है, क्योंकि पॉइंटर को पहले तत्व पर सेट नहीं किया जा सकता है। – Jonathan

0

php में एक foreach पाश मूल सरणी की एक प्रति अधिक पुनरावृति जाएगा, next() और prev() कार्यों बेकार बना रही है। आप एक साहचर्य सरणी है और अगले आइटम लाने के लिए की जरूरत है, तो आप सरणी चाबियाँ पर बजाय पुनरावृति सकता है:

foreach (array_keys($items) as $index => $key) { 
    // first, get current item 
    $item = $items[$key]; 
    // now get next item in array 
    $next = $items[array_keys($items)[$index + 1]]; 
} 

के बाद से चाबियों का परिणामी सरणी एक सतत सूचकांक में ही है, तो आप का उपयोग करने के बजाय का उपयोग कर सकते मूल सरणी

, जागरूक कि $next पिछले यात्रा के लिए null हो जाएगा बनें पिछले के बाद कोई अगले आइटम है के बाद से। गैर मौजूद सरणी कुंजियों तक पहुंचने से एक PHP नोटिस फेंक देगा। कि बचने के लिए, या तो:

  1. बताए मूल्यों से पहले अंतिम यात्रा के लिए जाँच करने $next
  2. चेक अगर index + 1 साथ कुंजी array_key_exists()

विधि 2 पूरा foreach का उपयोग करते हुए ऐसा दिखाई दे सकता के साथ मौजूद है :

foreach (array_keys($items) as $index => $key) { 
    // first, get current item 
    $item = $items[$key]; 
    // now get next item in array 
    $next = null; 
    if (array_key_exists($index + 1, array_keys($items))) { 
     $next = $items[array_keys($items)[$index + 1]]; 
    } 
} 
+1

प्रत्येक पुनरावृत्ति पर array_keys का उपयोग करना? यह सबसे धीमा समाधान होना चाहिए। – Semra

2

सामान्य समाधान एक कैशिंग इटरेटर हो सकता है। एक उचित ढंग से कार्यान्वित कैशिंग इटरेटर किसी भी इटरेटर के साथ काम करता है, और स्मृति बचाता है। PHP एसपीएल में CachingIterator है, लेकिन यह बहुत विषम है, और इसकी सीमित कार्यक्षमता है। हालांकि, आप इस तरह अपना खुद का लुकहेड इटरेटर लिख सकते हैं:

<?php 

class NeighborIterator implements Iterator 
{ 

    protected $oInnerIterator; 

    protected $hasPrevious = false; 
    protected $previous = null; 
    protected $previousKey = null; 

    protected $hasCurrent = false; 
    protected $current = null; 
    protected $currentKey = null; 

    protected $hasNext = false; 
    protected $next = null; 
    protected $nextKey = null; 

    public function __construct(Iterator $oInnerIterator) 
    { 
     $this->oInnerIterator = $oInnerIterator; 
    } 

    public function current() 
    { 
     return $this->current; 
    } 

    public function key() 
    { 
     return $this->currentKey; 
    } 

    public function next() 
    { 
     if ($this->hasCurrent) { 
      $this->hasPrevious = true; 
      $this->previous = $this->current; 
      $this->previousKey = $this->currentKey; 
      $this->hasCurrent = $this->hasNext; 
      $this->current = $this->next; 
      $this->currentKey = $this->nextKey; 
      if ($this->hasNext) { 
       $this->oInnerIterator->next(); 
       $this->hasNext = $this->oInnerIterator->valid(); 
       if ($this->hasNext) { 
        $this->next = $this->oInnerIterator->current(); 
        $this->nextKey = $this->oInnerIterator->key(); 
       } else { 
        $this->next = null; 
        $this->nextKey = null; 
       } 
      } 
     } 
    } 

    public function rewind() 
    { 
     $this->hasPrevious = false; 
     $this->previous = null; 
     $this->previousKey = null; 
     $this->oInnerIterator->rewind(); 
     $this->hasCurrent = $this->oInnerIterator->valid(); 
     if ($this->hasCurrent) { 
      $this->current = $this->oInnerIterator->current(); 
      $this->currentKey = $this->oInnerIterator->key(); 
      $this->oInnerIterator->next(); 
      $this->hasNext = $this->oInnerIterator->valid(); 
      if ($this->hasNext) { 
       $this->next = $this->oInnerIterator->current(); 
       $this->nextKey = $this->oInnerIterator->key(); 
      } else { 
       $this->next = null; 
       $this->nextKey = null; 
      } 
     } else { 
      $this->current = null; 
      $this->currentKey = null; 
      $this->hasNext = false; 
      $this->next = null; 
      $this->nextKey = null; 
     } 
    } 

    public function valid() 
    { 
     return $this->hasCurrent; 
    } 

    public function hasNext() 
    { 
     return $this->hasNext; 
    } 

    public function getNext() 
    { 
     return $this->next; 
    } 

    public function getNextKey() 
    { 
     return $this->nextKey; 
    } 

    public function hasPrevious() 
    { 
     return $this->hasPrevious; 
    } 

    public function getPrevious() 
    { 
     return $this->previous; 
    } 

    public function getPreviousKey() 
    { 
     return $this->previousKey; 
    } 

} 


header("Content-type: text/plain; charset=utf-8"); 
$arr = [ 
    "a" => "alma", 
    "b" => "banan", 
    "c" => "cseresznye", 
    "d" => "dio", 
    "e" => "eper", 
]; 
$oNeighborIterator = new NeighborIterator(new ArrayIterator($arr)); 
foreach ($oNeighborIterator as $key => $value) { 

    // you can get previous and next values: 

    if (!$oNeighborIterator->hasPrevious()) { 
     echo "{FIRST}\n"; 
    } 
    echo $oNeighborIterator->getPreviousKey() . " => " . $oNeighborIterator->getPrevious() . " ----->  "; 
    echo "[ " . $key . " => " . $value . " ]  -----> "; 
    echo $oNeighborIterator->getNextKey() . " => " . $oNeighborIterator->getNext() . "\n"; 
    if (!$oNeighborIterator->hasNext()) { 
     echo "{LAST}\n"; 
    } 
} 
संबंधित मुद्दे