paruby Posted November 28, 2008 Share Posted November 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134579-solved-getting-individual-array-items-from-function-and-traverse-array/ Share on other sites More sharing options...
vbnullchar Posted November 28, 2008 Share Posted November 28, 2008 <?php function arr($array, $index = NULL) { if(!is_null($index)) { return $array[$index] } return($array); } ?> Link to comment https://forums.phpfreaks.com/topic/134579-solved-getting-individual-array-items-from-function-and-traverse-array/#findComment-700760 Share on other sites More sharing options...
genericnumber1 Posted November 28, 2008 Share Posted November 28, 2008 Yes, you can set the array to a session variable. Link to comment https://forums.phpfreaks.com/topic/134579-solved-getting-individual-array-items-from-function-and-traverse-array/#findComment-700762 Share on other sites More sharing options...
paruby Posted November 28, 2008 Author Share Posted November 28, 2008 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 Link to comment https://forums.phpfreaks.com/topic/134579-solved-getting-individual-array-items-from-function-and-traverse-array/#findComment-701212 Share on other sites More sharing options...
genericnumber1 Posted November 29, 2008 Share Posted November 29, 2008 just do it? <?php function test() { return array('blah', 'blah2'); } $var = test(); $var[1]; // blah2 ?> Link to comment https://forums.phpfreaks.com/topic/134579-solved-getting-individual-array-items-from-function-and-traverse-array/#findComment-701398 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.