Jump to content

[SOLVED] getting individual array items from function, and traverse array


Recommended Posts

There are 2 parts to this question please...

 

1.  I have a function that creates an array.  I can call the function and return the full array.  But how do I reference a single item in the array, given the index?

 

Here is my code:

 

function createMyArray()
{
   
    $files = array();	// Create Array

    // Do some stuff to populate the array

    return($files);
}

print_r(createMyArray());

 

When I run the above code, I get the full array returned.  I want to be able to return a given index... ?

 

2.  Once I do that, I want to be able to traverse through the array and show each index value on its own page, so to speak, so page 1 displays $files[0], page 2 displays $files[1], etc., without having to run through the function again.  Can this be done for instance by setting the array as a session variable??

 

Thanx!

 

Pete

After seeing vbnullchar's response, I realized I asked the question wrong.  I want to return the full array from the function, then outside the function, reference one index or another...  This changes the answer, as vbnullchar thought I wanted to rturn 1 index from the function...  Sorry for the confusion.

 

Pete

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.