By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Getting array information out of a function on into another array

P: n/a


I need to find a way to transfer all the values of an array inside a
function out of the fuction into another array. IE

function splice($filename){
if(file_exists($filename)){
$value=array("0"); // clear the array.
$rows=file($filename); // break text file into a rows array
for($num=0;$num < count($rows); ++$num){
$column=split(";",$rows[$num]);
$value[$num]=$column;}}
return $value;
}

$array1 = splice(file.txt)
Example file data would be

data1;an1;bc1
data2;an2;bc2
data3;an3;bc3

How do I go about getting the function to pass all it's data to the variable
$array1?
Thanks in advance.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

// Note the & in &$value when the function is declared below
// It means pass by reference - Later, we call splice function
// with $array1 which has its value passed to $value. Splice
// changes $value and the & returns the changes to your call
function splice($filename, &$value){
if(file_exists($filename)){
// Your declaration below does *not* clear the array - use
// unset if you need to - see what I have done just before
// the call to splice near the bottom of this post
// $value=array("0"); // clear the array.
$rows=file($filename); // break text file into a rows array
for($num=0;$num < count($rows); ++$num){
$column=split(";",$rows[$num]);
$value[$num]=$column;}}
return $value;
}

// Splice file.txt - pass the function $array1
// $array1 should be empty and have no previous value
// hence why we make sure using an 'unset()'
unset($array1); // Clear $array1 from having *any* value
splice(file.txt, $array1)

Note your method of reading $rows could perhaps be done alot better...
Have you entertained the idea of using explode(), implode() or even
fgetcsv() ????

The latter is bound to be of help and be more friendly on your systems
resouces... see http://www.php.net/fgetcsv for more info

laters
randelld

Jul 17 '05 #2

P: n/a
Thanks appreciate the help,

I'm fairly new to php so I will definatly look at the fgetcsv which would
probably easier to use than a text file.

-Patrick-

"Reply Via Newsgroup" <re****************@please.com> wrote in message
news:Z46hc.175186$Pk3.135529@pd7tw1no...

// Note the & in &$value when the function is declared below
// It means pass by reference - Later, we call splice function
// with $array1 which has its value passed to $value. Splice
// changes $value and the & returns the changes to your call
function splice($filename, &$value){
if(file_exists($filename)){
// Your declaration below does *not* clear the array - use
// unset if you need to - see what I have done just before
// the call to splice near the bottom of this post
// $value=array("0"); // clear the array.
$rows=file($filename); // break text file into a rows array
for($num=0;$num < count($rows); ++$num){
$column=split(";",$rows[$num]);
$value[$num]=$column;}}
return $value;
}

// Splice file.txt - pass the function $array1
// $array1 should be empty and have no previous value
// hence why we make sure using an 'unset()'
unset($array1); // Clear $array1 from having *any* value
splice(file.txt, $array1)

Note your method of reading $rows could perhaps be done alot better...
Have you entertained the idea of using explode(), implode() or even
fgetcsv() ????

The latter is bound to be of help and be more friendly on your systems
resouces... see http://www.php.net/fgetcsv for more info

laters
randelld

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.