Re: array in PHP

From: d (d_at_example.com)
Date: 12/03/04


Date: Fri, 03 Dec 2004 14:39:07 GMT

Well, this line empties the array:

$Lijst=Leesnamen($Moeder, ($n*2)+1);

as you're putting the return value of Leesnamen() straight into the array,
not into the element you're looping through. I can see from the function
that it changes the $Lijst array, but I can't see how it can read that
variable, as it's outside the scope of that function. Using a "global
$Lijst;" in the Leesnamen() function, will let you see and alter that array.

Now, you can change the $Lijst=Leesnamen() line, removing the '$Lijst='
part, as the function in question doesn't return anything.

Try that!

d.

"Piet" <p.potjer@chello.nl> wrote in message
news:8O_rd.1799$yn4.1409@amsnews03-serv.chello.com...
>I let it print (see insertion) as a test
> "d" <d@example.com> schreef in bericht
> news:jD_rd.79663$38.17955@fe2.news.blueyonder.co.uk...
>> Cool :) Where do you notice your array is missing variables?
>>
>> "Piet" <p.potjer@chello.nl> wrote in message
>> news:Vz_rd.1736$yn4.972@amsnews03-serv.chello.com...
>>> This is the exact code - with comments in Dutch.
>>> "d" <d@example.com> schreef in bericht
>>> news:_p_rd.79653$38.1588@fe2.news.blueyonder.co.uk...
>>>> Is that the exact code you're using? There are quite a few problems
>>>> with it, like the missing $ from variables, missing closing brackets,
>>>> etc... it most definitely won't run :)
>>>
>>> Function MaakLijst ($idnr){
>>> global $Lijst;
>>> $m=1;
>>> $Lijst = Leesnamen($idnr, $m);
> Print $Lijst[1]["Naam"]; // Result: Pete
>>> for ($n=1; $n<=7;$n++){
>>> $Vader=$Lijst[$n]["Vader"];
>>> $Moeder=$Lijst[$n]["Moeder"];
>>> if ($Vader!="1"){
>>> $lijst=Leesnamen($Vader, $n*2);
>>> $Lijst=Leesnamen($Moeder, ($n*2)+1);
>>> }
> Print $Lijst[1]["Naam"]; // Result: Empty
> Print $Lijst[2]["Naam"]; // Result: Chris
>
>>> }
>>> return $Lijst;
>>> }
>>> Function LeesNamen($idnr, $m) {
>>> $x=False; // Variabele laat while lus niet onnodig lang doorlopen
>>> //stel de variabelen voor de toegang tot de database in
>>> $Host = "localhost";
>>> $Gebruiker = "User";
>>> $Wachtwoord = "";
>>> $DBNaam = "Parenteel";
>>> $Tabelnaam="familieleden";
>>> $Verbinding=mysql_connect($Host, $Gebruiker, $Wachtwoord);
>>> $Opdracht = "Select * from $Tabelnaam";
>>> $Resultaat =mysql_db_query ($DBNaam, $Opdracht, $Verbinding);
>>> //Haal de resultaten uit de database
>>> while (($Rij = mysql_fetch_array ($Resultaat)) && ($x==FALSE)) {
>>> if ($Rij[id]==$idnr) {
>>> $Lijst[$m]["Naam"]=$Rij[Naam];
>>> $Lijst[$m]["Vader"]=$Rij[Vader];
>>> $Lijst[$m]["Moeder"]=$Rij[Moeder];
>>> $x=True;
>>> }
>>> }
>>> $x=False;
>>> mysql_close($Verbinding);
>>> }
>>> return ($Lijst);
>>> }>> "d" <d@example.com> schreef in bericht
>>>>> news:5c_rd.69942$F7.2426@fe1.news.blueyonder.co.uk...
>>>>>> Can you copy and paste your exact script here? It seems something
>>>>>> very subtle is happening :)
>>>>>>
>>>>>> "Piet" <p.potjer@chello.nl> wrote in message
>>>>>> news:c1_rd.56485$lN.37579@amsnews05.chello.com...
>>>>>>> You're right: in my message I did not mention the quotes. I did in
>>>>>>> my script. Sorry.
>>>>>>> So the problem remains: The content of List[1]["Name"} disappears
>>>>>>> when I call the function again.
>>>>>>>
>>>>>>> "d" <d@example.com> schreef in bericht
>>>>>>> news:PqZrd.79502$38.8284@fe2.news.blueyonder.co.uk...
>>>>>>>> For one thing, you should put your textual keys in quotes, so
>>>>>>>> $List[1][Name] becomes $List[1]["Name"]. PHP shouldn't support the
>>>>>>>> method you use (as, technically, Name should be a constant in your
>>>>>>>> script, not a textual key) - it just does to prevent people's code
>>>>>>>> from breaking :)
>>>>>>>>
>>>>>>>> Try changing that and see if it helps.
>>>>>>>>
>>>>>>>> If not, put some values in your array then put this command at the
>>>>>>>> very end of your script:
>>>>>>>>
>>>>>>>> var_dump($List);
>>>>>>>>
>>>>>>>> It will display the contents of the $List variable, letting you see
>>>>>>>> if things are as they should be.
>>>>>>>>
>>>>>>>> d
>>>>>>>>
>>>>>>>> "Piet" <p.potjer@chello.nl> wrote in message
>>>>>>>> news:jjZrd.56081$lN.17932@amsnews05.chello.com...
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> I try to use a two-dimension array in a function.
>>>>>>>>> It is declared as global (global $List).
>>>>>>>>> When it is filled the first time everything seems to be okay:
>>>>>>>>> $List[1][Name]="Pete";
>>>>>>>>> When I call the function again I fill it like
>>>>>>>>> $List[2][Name]="Chris";
>>>>>>>>> The result seems good, but the foolish effect is that
>>>>>>>>> $List[1][Name] is empty then.
>>>>>>>>> What is going wrong? Can anybody help me?
>>>>>>>>> (I use PHP 4.3.8.)
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>



Relevant Pages

  • Re: Arrays to replace very slow loops ?
    ... accomplished by looping through ranges rather than looping through ... That is, with either the range or the array, n is the row ... >>>difference between looping through arrays and looping through ranges) is ... >> no built in method for deleting a "row" in an array without ...
    (microsoft.public.excel.programming)
  • Re: Performance Question
    ... Split is more efficient than actually looping through the string. ... second time (parsing it yet again) to assign the data to the array. ...
    (microsoft.public.vb.general.discussion)
  • Re: Fastest way to find item in an array.
    ... first putting cell values into an array rather than looping cells. ...
    (microsoft.public.excel.programming)
  • Re: Arrays to replace very slow loops ?
    ... That is, with either the range or the array, n is the row ... >>difference between looping through arrays and looping through ranges) is ... > decision not match the new layout of the data on the worksheet. ...
    (microsoft.public.excel.programming)
  • Re: IsArray doesnt work with array var populated with xxx.GetRows()
    ... > I agree looping through an array is quicker than looping through a ... but don't forget the cost of getting the array from the ... > already existing recordset. ... A disconnected recordset is not needed to put data into xml. ...
    (microsoft.public.inetserver.asp.general)