2010-08-04 21 views
0

得到當前數組的指針位置,而這樣做的foreach $對象 - > result_array()

嗨, 考慮這個,的情況下。

foreach($object->result_array() as $rs) 
{ 
    //how do i display the current array element index that is the iteration index. 
    //i want to call a function when the iterator is in the last-1 position. 
    // and more specifically i want to use it in this place and not any other for(;;) loops. 
    // and even it is an example with other loops then that is fine. 
} 

應該有一個選項或使用current()函數。 我曾嘗試過,但我仍然需要做一些分析... 而不是調試和追蹤數組元素我張貼此線程。

以上所有可以用for循環做到嗎?

回答

2

添加=> $key這樣拿到鑰匙,以及:

foreach($object->result_array() as $rs => $key) 
{ 
    echo $key; 
} 

和高於一切是有可能做到這一點 一個for循環?

是這樣的:

for($i = 0; $i < count($object->result_array()); $i++) 
{ 
    // your code... 
} 
+0

工作。謝謝。但... 你說的是對的,但似乎你只想指點一些想法,而不是提供更多的代碼。 無論如何,我得到了我想要的,我已經投票給你。謝謝。 在那個代碼中,我想要$ rs,它是數組位置或迭代器的當前索引號。 在for循環中,我們需要做$ object-> result_array [$ i] ['field'] 它計算出了兩者,但您可以給出這樣的結果。 無論如何,我嘗試了自己,並通過這種方式讓我們有我們的分析尖銳。 我從你的代碼中假定了這個。 – 2010-08-04 08:37:34

+0

無論如何,我得到了我想要的,我已經投票了你的答案。謝謝。 – 2010-08-04 08:39:14

+0

@Jayapal Chandran:這是個好消息,我儘可能多地回答你的問題。很高興看到你自己算出了剩餘的部分,這對程序員來說總是一件好事:) – Sarfraz 2010-08-04 08:40:15