Jump to content


Photo

passing multi-dimensional arrays into functions


  • Please log in to reply
6 replies to this topic

#1 heuristic

heuristic
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 16 May 2006 - 05:40 PM

hi!
I am very new to php with a strong C/C++ background. I currently have a C code which i want to port to php. The code involves passing 2-d arrays into functions. Is it possible to do that in php ( I dont want to use associative arrays. i.e I want to use numerical subscripting for the arrays )

The C code is something like this :

void myFunc( int myArray[2][3] )
{
      for( int i=0; i<2; i++ )
      {
           for( int j=0; j<3; j++ )
                  cout << myArray[i][j] << " ";
           cout << endl;
      }
}

This is what i tried to do in php :

<?php

function printArray( &$ar )
{
    print("printArray");
    print("<hr>");

     for( $i=0; $i<2; $i++ )
    {
        for( $j=0; $j<3; $j++ )
        {
            $v = $ar[$i][$j];
            print("$v ");
        }
        print("<br>");
    }

    print("<hr>");
}

    $myarray[0][0] = 0;
    $myarray[0][1] = 1;
    $myarray[0][2] = 2;
    $myarray[1][0] = 10;
    $myarray[1][1] = 11;
    $myarray[1][2] = 12;

    printArray( $myArray );
    
?>

Why doesnt this work?


#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 06:17 PM

did you get any errors? the code looks totally correct to me.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 May 2006 - 06:34 PM

Variable names are case-sensitive

$my[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]a[!--colorc--][/span][!--/colorc--]rray[0][0] = 0;

printArray( $my[!--coloro:#FF6666--][span style=\"color:#FF6666\"][!--/coloro--]A[!--colorc--][/span][!--/colorc--]rray );
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 06:48 PM

nice catch Barand!
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 May 2006 - 11:10 PM

what does the & in function printArray( [!--coloro:red--][span style=\"color:red\"][!--/coloro--]&[!--colorc--][/span][!--/colorc--]$ar ) do?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 May 2006 - 11:16 PM

It passes the array "by reference" .

Instead of passing a copy of the array to the function, it passes a reference to point to the original array. It's more efficient. Any changes you make to the array inside the function will be made to the original array.

Its like

print_array() {
     global $myarray;

     ...
}

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 12:31 AM

oh okay i get it. actually, now that you mentioned it, i remembered it. I guess I haven't yet made a function that has needed to do that, so i forgot. Every function I've done so far has just used var/array to retrieve info from db or else blend it up and return some new var(s). I knew it looked vaguely familiar. thx.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users