Title: Foreach: Get a particular iteration value
Tags: php;codeigniter;foreach
Question: I am having four images inside a code igniter foreach loop.For one of my project, I want to take image of the third iteration from the loop. Is it possible to take the particular iteration? I hope it is a weird question . But, I need to take separate iteration values, for my separate <div&gt; in my view of MVC.
The following is the result, coming from controller.
array (size=4)
0 =&gt;
array (size=1)
'file_name' =&gt; string 'PHP_Petty_shop12.jpg' (length=20)
1 =&gt;
array (size=1)
'file_name' =&gt; string 'Manavanur_innagration123.jpg' (length=28)
2 =&gt;
array (size=1)
'file_name' =&gt; string 'SHG_Group_meeting6.jpg' (length=22)
3 =&gt;
array (size=1)
'file_name' =&gt; string 'Low_cost_toile17.jpg' (length=20)
The following is the code which i tried to get the iterated value..
<?php foreach($slider_info as $key=&gt;$slider)
{ ?&gt;
<div class="carousel-inner" role="listbox"&gt;
<div class="item <?php echo ($key == 4) ? "active":""; ?&gt; "&gt;
<img style="height: 300px;width:100%;" src="<?php echo base_url();?&gt;upload/sliderimages/<?php echo $slider['file_name']; ?&gt;" height="100" alt="Image of every carousel"/&gt;
</div&gt;
</div&gt;
<?php } ?&gt;
I am not able to get the desired output. Is there a way?
Comment: Directly access `$slider_info[3]`…?!

Comment: Well, again: instead of doing a `foreach` and "accessing the 3rd iteration"… Just directly access `$slider_info[3]`…!? `$slider = $slider_info[3];`

Comment: Why don't you check for `$key==2` ?

Comment: So your issue is resolved then.

Comment: @Keynes: Can you explain in detail, what you need exactly?

Comment: But what is your purpose?

Comment: @Keynes: Try " > , And see what happpens.

Comment: yes one time 3rd iteration after that exit

Comment: if its not possible, i will have to delete this question yaar

Comment: @deceze, Is that possible yaar

Comment: @deceze, it shown me an error message as `Array to String Conversion` Error

Comment: @Rahul, yes doing that provides the third image

Comment: yes indeed yaar, just post in your answer, in such a way that the key is different in the loop `foreach($slider_info as $key=>$slider)`

Comment: @AshokGujjar, i want to take a 3rd iteration from the for each loop

Comment: @AshokGujjar, in the bottom of the gallery page, want to show a image which comes in the 3rd iteration..



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 15, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:06/01/17', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<php><codeigniter><foreach>', 'Title': 'Foreach: Get a particular iteration value', 'ViewCount': 125, 'id': 44299406, 'user_AboutMe': '<p>Simply passionate about PHP, Codeigniter. Developing cutting edge Web applications using PHP’s wealth of frameworks. Just Keep Calm and Love WordPress. Loves MySQL and MS-SQL Querying, etc</p>\n', 'user_AccountId': 7620081.0, 'user_DisplayName': 'Deepak Keynes', 'user_Reputation': 2071.0, 'user_Views': 440.0, 'user_id': 5778347.0}}