March 10, 2012

Iterate through session array?

Question by 3D-kreativ

I haven’t use PHP for some time, and I need some help to create some lines of code. I wrote this line of code some years ago for a website


This is my problem, I just want to iterate through the array and put the first value at index 0 to $buildingId and then next time put the value of index 1 to $buildingId with som While loop i guess? I know it’s easy, but sadly I haven’t used PHP for some time! Preciate the help! Thanks!

Answer by Starx

Use foreach()

foreach($_SESSION['buildingsAlreadySeenList'] as $id => $value) {
    echo $id; // this is the index and for the first time this is like to give o if the indexes were not manipulated somehow

    //put the index value to buildingid
    $buildingId = $id;
    echo $value; //the actual value of the array


To check if the array is empty count the variables

if(count($_SESSION['buildingsAlreadySeenList'])) { 
   //array is not empty
} else {
   //array is empty

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!