473,659 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Having a problem returning an array from a function

30 New Member
I'm writing code for a class where I've set up functions to load data from a file in the same directory as the PHP file (using the "file" method). Then, the goal is for the program to take the info from that function and, 1) search for a match from user-inputted names, 2) replace the old name (scanned by the second function) with the new name, and 3) save it back to the original file. The file contains info that was input to it by another PHP page, but that really doesn't matter. The problem is that I wrote the following test code to retrieve the info from the file, but it's not working properly.

<?php
function loadData()
{
if(!$filename = file("employees .dat"))
{
echo('Error while opening file');
}
else
{

$numLines = count($filename );
for ($i = 0; $i < $numLines; $i++)
{
$filename[$i];
}
return array($filename );
}
}

$myFile = loadData();
echo($myFile);
?>

Again, this is just a test code. The ultimate aim is not to print the array to the screen, but to retrieve the data from the function so that it can be passed on to the next function that will scan the array to see if an input from the user matches an existing name in the file. I can post the rest of the code after I get this part working correctly.

P.S. The result of the function as it stands right now, is the word "Array" printed at the top left of the screen and nothing else. [It appears that it's returning the return TYPE instead of the actual values of the elements in the array]. Any help would be much appreciated, as all the other posts about similar issues seemed to be about databases, and not applicable. Thanks.
Feb 26 '07 #1
29 2300
xwero
99 New Member
I'm writing code for a class where I've set up functions to load data from a file in the same directory as the PHP file (using the "file" method). Then, the goal is for the program to take the info from that function and, 1) search for a match from user-inputted names, 2) replace the old name (scanned by the second function) with the new name, and 3) save it back to the original file. The file contains info that was input to it by another PHP page, but that really doesn't matter. The problem is that I wrote the following test code to retrieve the info from the file, but it's not working properly.

<?php
function loadData()
{
if(!$filename = file("employees .dat"))
{
echo('Error while opening file');
}
else
{

$numLines = count($filename );
for ($i = 0; $i < $numLines; $i++)
{
$filename[$i];
}
return array($filename );
}
}

$myFile = loadData();
echo($myFile);
?>

Again, this is just a test code. The ultimate aim is not to print the array to the screen, but to retrieve the data from the function so that it can be passed on to the next function that will scan the array to see if an input from the user matches an existing name in the file. I can post the rest of the code after I get this part working correctly.

P.S. The result of the function as it stands right now, is the word "Array" printed at the top left of the screen and nothing else. [It appears that it's returning the return TYPE instead of the actual values of the elements in the array]. Any help would be much appreciated, as all the other posts about similar issues seemed to be about databases, and not applicable. Thanks.
to print an array to the screen you have to use print_r instead of echo.

Your code looks fine.
Feb 26 '07 #2
Motoma
3,237 Recognized Expert Specialist
I'm writing code for a class where I've set up functions to load data from a file in the same directory as the PHP file (using the "file" method). Then, the goal is for the program to take the info from that function and, 1) search for a match from user-inputted names, 2) replace the old name (scanned by the second function) with the new name, and 3) save it back to the original file. The file contains info that was input to it by another PHP page, but that really doesn't matter. The problem is that I wrote the following test code to retrieve the info from the file, but it's not working properly.

<?php
function loadData()
{
if(!$filename = file("employees .dat"))
{
echo('Error while opening file');
}
else
{

$numLines = count($filename );
for ($i = 0; $i < $numLines; $i++)
{
$filename[$i];
}
return array($filename );
}
}

$myFile = loadData();
echo($myFile);
?>

Again, this is just a test code. The ultimate aim is not to print the array to the screen, but to retrieve the data from the function so that it can be passed on to the next function that will scan the array to see if an input from the user matches an existing name in the file. I can post the rest of the code after I get this part working correctly.

P.S. The result of the function as it stands right now, is the word "Array" printed at the top left of the screen and nothing else. [It appears that it's returning the return TYPE instead of the actual values of the elements in the array]. Any help would be much appreciated, as all the other posts about similar issues seemed to be about databases, and not applicable. Thanks.

To get the program to echo the actual data, the command you will need is print_r()
Feb 26 '07 #3
tnspc
30 New Member
Thanks for your input. So, the last line would be print_r($myFile );?
Feb 26 '07 #4
Motoma
3,237 Recognized Expert Specialist
Thanks for your input. So, the last line would be print_r($myFile );?
Yup. Try it and post your results if you have problems.
Feb 26 '07 #5
tnspc
30 New Member
Thanks for your advice! It did display, although the main reason I had it display was to make the sure the array was being passed from the function to the main code. Now, on to problem #2! :o)

The second function in this program takes the $myFile array passed from the previous function and loops through the elements to look for a match from the user-input form. The variable from the form is $oldName. Inside the function, I declare the local versions of those two variables (see comments on function). The professor wants us to return either the index of the array element that contains the match or -1 if no match is found. I have a loop that checks the array, but so far, it's not working. If I put a return outside the loop of -1, then nothing is found, and if I put it in the else clause, it only checks the first element of the array. Here's the code [note: I took the return -1 out already because I knew it wasn't working]:

function searchData($myF ile, $oldName)
{
$a = $myFile;
$s = $oldName;

$numElements = count($a);
for($i = 0; $i < $numElements; $i++)
{
if(eregi($oldNa me, $myFile))
{
return $i;
}
else
$i++;
}
}


It's called by the following line in the main body of the code:
$index = searchData($myF ile, $oldName);

*The reason we need to do it as an index or -1 is because that return value is used in the next if statement in the body that sets up the use of the last two functions to replace oldName with newName, and to write the data back to the original file accessed in the first function. I've tried a do-while loop as an alternative, but it didn't work either. Any suggestions?
Feb 26 '07 #6
Motoma
3,237 Recognized Expert Specialist
How about this:

[php]
function searchData($myF ile, $oldName)
{
$a = $myFile;
$s = $oldName;

$numElements = count($a);
for($i = 0; $i < $numElements; $i++)
if(eregi($oldNa me, $myFile)) return $i;
return -1;
}
[/php]
Feb 26 '07 #7
tnspc
30 New Member
Didn't work. Always returns -1, even if there's a match.
Feb 26 '07 #8
Motoma
3,237 Recognized Expert Specialist
My Bad:

[php]
function searchData($myF ile, $oldName)
{
$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
if(eregi($oldNa me, $myFile[$i])) return $i;
return -1;
}
[/php]

You forgot to put the index in $myFile that you wanted to compare to.
Feb 26 '07 #9
tnspc
30 New Member
Yeah, I noticed that and changed it, and it still did not work... it will return -1 unless the match is found with the first element (I believe). That's the problem I ran into that I haven't been able to solve yet. It needs to break out of the loop and return the index of the matching element if it finds it, or return -1 after the loop if no match. It may just be a problem with the placement of the return -1 statement; but, in another version of the code, I put it outside the loop brackets, and it was executing that statement every time, instead of returning from the function in the event of a match. For example:

function searchData($myF ile, $oldName)
{
$a = $myFile;
$s = $oldName;
{
$numElements = count($a);
for($i = 0; $i < $numElements; $i++)
{
if(!(eregi($s, $a[$i])))
{
return $i;
}
else
{
$i++;
}
}
}

return -1;
}
Feb 27 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
14016
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
7
7292
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic array e.g: array *a; a = function(...)
12
2748
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day trying to get this to work, but been unable to solve my mistake. What the function should return is a file name used for creating logs files. It will look something like JN122345.log
41
3801
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x = rand(); y = rand();
3
1847
by: Carramba | last post by:
hi! the code is cinpiling with gcc -ansi -pedantic. so Iam back to my question Iam trying to make program were I enter string and serach char. and funktion prints out witch position char is found this is done if funktion serach_char. so far all good what I want do next is: return, from funktion, pointer value to array were positions ( of found char) is stored. and print that array from main. but I only manage to print memory adress to...
13
4783
by: Jordan Tiona | last post by:
Is this not allowed in C++? I try in MSVC, but I always get an error, and the function no longer shows up in my class view.
17
3240
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
0
1034
by: ppuniversal | last post by:
hello, I am making an application where I have to copy the values of some of the tuples(which are not already copied into the files) from my database in MySQL into two files.Now I have an attribute with name "Replication_Done". This is an integer type of attribute and its initial value is 0, meaning that this tuple has not been copied into any of the 2 files. Now whenever a tuple is copied into one of the file, the value of this...
0
8428
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.