How can I reset an array in PHP?

Answer Posted / jude jeevanraj.p

Sometimes if you are moving through the various elements in
an array, you will want to return the pointer to the
beginning of the array.

This is easily done with the reset function, like this:
reset($array);

This returns the first element and sets it as the current
element.

Here is a little example just to make this technique clear:

<?php
$numbers = array("Item 1","Item 2","Item 3");
next($numbers);
$thisvalue = current($numbers); echo "We are now at
$thisvalue\n\n";
$first = reset($numbers);
echo "Back to $first";
?>

Is This Answer Correct ?    21 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are different types of runtime errors in php?

551


Do you know what is the difference between mysql_fetch_object() and mysql_fetch_array()?

526


When viewing an html page in a browser, the browser often keeps this page in its cache. What can be possible advantages/disadvantages of page caching? How can you prevent caching of a certain page (please give several alternate solutions)?

557


4 down vote favorite share [g+] share [fb] share [tw] I am developing my site using server side sessions using redis as backend for saving the session. Now the issue which is bothering me is of user leaving the website without logging out. I mean user simply closes the browser which causes the cookie to be deleted. Now session of that user still exists on the server and will not be used again as new login requires creating a new session due to security reasons. To avoid the case where hacker steals the old cookie and use it after user login again with same old session id. In essence user leaves the website without explicitly logging out and his session will be deleted after certain time limit of inaccessibility. I am thinking time limit of 30-60 minutes. Also with every new request from user his cookie will also be updated to keep track of when the user last time accessed the site. But nowadays, people let site remain open for long time without accessing it. For example users open facebook and gmail in new tabs and forget about them for 2-3 hours and still they are not asked to login again. Is letting a 2-3 hours old cooke access the session secure? My concern is someone steals user cookie and use it 2-3 hours later. Thinking on this topic has also forced me to question how facebook manages security if user can use a session where they are not accessing it for long periods of time and still they remain logged in. Or is it not secure for me to keep logged in when am not accessing the site session for longer period of time? It can be the case also there is some pinging mechanism using which sites keep track of user having their site open in a browser and when browser closes they are notified and can work accordingly. My website is a social network and needs all those security and usage features which a social network may need. I am new to web security and web development in general and may be the case where my above questions may seem a little basic. If you feel that is the case kindly point to some good reference where I can read and find answers to my question.

1743


How can php and html interact?

622






What is use of htmlspecialchars php?

520


Which is used to maintain the value of a variable over different pages?

572


What is difference between include and include_once in php?

512


What is the function func_num_args() used for?

531


Which is the latest version of php?

549


What are the differences between echo and print?

562


What is $_ files in php?

565


How can we calculate the similarity between two strings?

581


Can we use get instead of post?

500


What is nginx and what is it used for?

516