473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting array information out of a function on into another array



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
2 2395

// 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: iannorton | last post by:
Hi, I've spent most of today trying to solve this problem, but sadly no luck. I have an shopping basket based on an array which stores the name, product id, quantity and price for products, i...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
3
by: Michael Glass | last post by:
I'm working on an ASP.Net web app using VS2005 and the .Net 2.0 framework, and I have a serious problem with the page I'm currently working on. The page has, among other things, two FormViews and a...
4
by: chippy | last post by:
I am finding that when I use the cloneNode method to copy an HTML element that contains a <script> tag, the contents of the <script> tag, (ie. the javascript) are removed. If I do this: var...
3
by: Andrew Gentile | last post by:
Hello, I am trying to write a function which takes several floats as inputs, then returns 1x6 array of floats. I normally program in Matlab, and I am often confused by C. It seems that this...
6
by: sathyashrayan | last post by:
Dear Group, Please look at the following demo link. http://www.itsravi.com/demo/new_pms/admin/addproject.php
9
by: KDawg44 | last post by:
Hi, I have PHP function that adds a record to the database. The table has an ID that is AUTO_INCREMENT. Is there anyway to get that ID back when I do any kind of insert? That ID is a foreign...
9
Catalyst159
by: Catalyst159 | last post by:
I have a form which is used to calculate residential Floor Area Ratio (FAR). The form is structured into seven parts as follows: Part A: Maximum FAR and Floor Area: Part B: Gross Floor Area of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.