473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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($filenam e){
if(file_exists( $filename)){
$value=array("0 "); // clear the array.
$rows=file($fil ename); // 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 2408

// 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($filenam e, &$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($fil ename); // 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.1751 86$Pk3.135529@p d7tw1no...

// 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($filenam e, &$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($fil ename); // 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
1799
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 want to take the Information in the array and output the Quantity and Product ID information into variables so that i can pass them into a Select...
6
22500
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was...
21
1733
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. However i don't know how to get the contents of the array out? I am used to passing parameters to a function, and getting a single result back. Is...
3
3866
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 GridView control, each with its own SqlDataSource. FormView1 talks to my Opportunity table and has an ItemTemplate and an EditItemTemplate....
4
4574
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 form1 = document.getElementById(sID).firstChild.cloneNode(true); alert(form1.outerHTML); I can see the empty <script> tags. I am wondering if there...
3
15638
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 should be an easy task, but I can't seem to get it done. Can someone please include a brief snippet of code which demonstrates how to get a function...
6
2486
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
1740
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 key that I need to do an insert on afterwards. Thanks. Kevin
9
2607
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 the main floors of the main house: Part C: Gross Floor Area of the basement or cellar: Part D: Gross Floor Area of the attic:
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.