SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot nicc9's Avatar
    Join Date
    Jan 2005
    Location
    New Orleans, LA
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile passing array as parameter

    hi all.

    i kinda know one can't pass an array as a parameter, as it has to be text, or an interger (doesn't it?).

    i got this piece of code that i want to turn into a function. basically i collect a lot of data related to the same object, and then i insert the info onto a database.

    the fields are like 20, so having 20 parameters sounds a little crazy, and would make it easy to make some mistake.

    the only solution i thought about would be to put all the data into an array, using it from the function as a global var, and the parameter would just be the array name. kind like this:

    HTML Code:
    function function_name($array_name) {
    global $array_name;
    // do stuff
    }
    
    $array_name = array();
    $array_name['foo'] = "some value";
    $array_name['val'] = "some other value";
    
    function_name($array_name);
    what do you guys think?

    is there a better way?

    thanks!

  2. #2
    SitePoint Evangelist optl's Avatar
    Join Date
    Oct 2004
    Location
    Washington DC
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can pass an array as a parameter.
    For the phrase "Bethesda home architect", my clients
    websites occupy 6 of the first 8 results
    on the 1st page of Google. My Secret SEO Strategy Revealed

  3. #3
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the whole array as parameter thing is more of a JavaScript thing, and most JavaScript parsers throw a right moody about even trying.

    PHP does not object, although I am not sure if this is because of a difference in the way that PHP handles arrays, or merely that PHP is nicer.

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nicc9 View Post
    hi all.
    the only solution i thought about would be to put all the data into an array, using it from the function as a global var, and the parameter would just be the array name. kind like this:

    HTML Code:
    function function_name($array_name) {
    global $array_name;
    // do stuff
    }
    
    $array_name = array();
    $array_name['foo'] = "some value";
    $array_name['val'] = "some other value";
    
    function_name($array_name);
    As far as i know, yes you can pass the whole array to the function. And if you pass the array there is no meaning of making the array global. If you make the array global, then you don't have to pass as parameter to the function since you can directly use it.

    If you mean that the changes in the array inside the function should come back to the main system, you can pass the array as reference with & like this:
    PHP Code:
        $arr = array();
        
    $arr[0] = 1;
        
        function 
    myfunction(&p$arr){
            
    $parr[1] = 2;
            
    // do other actions
        
    }
        
        
    myfunction($arr);
        
        
    print_r($arr); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    If you make the array global, then you don't have to pass as parameter to the function since you can directly use it.
    While this is technically true, it's generally consider very bad practise to pass messages through global variables. As a matter of fact, almost any use of global variables is a bad practise.

    Quote Originally Posted by rajug View Post
    If you mean that the changes in the array inside the function should come back to the main system, you can pass the array as reference with & like this:
    Again a rather bad practise; Using references can make your application very unpredictable. Have your function return the array instead.

    Quote Originally Posted by Php_penguin View Post
    the whole array as parameter thing is more of a JavaScript thing, and most JavaScript parsers throw a right moody about even trying.
    I don't know where you got that idea from?

  6. #6
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got that idea from having javascript piss me off for over an hour because i was trying to pass an array.

    It was an array with about 40 keys, numbered 0-39 of course, and FF wouldn't let me pass it to a function from within another function

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Php_penguin View Post
    i got that idea from having javascript piss me off for over an hour because i was trying to pass an array.

    It was an array with about 40 keys, numbered 0-39 of course, and FF wouldn't let me pass it to a function from within another function
    Javascript can be quite un-cooperative at times, but there is no preventing you from passing an array as parameter. Most likely you had some minor syntctical error in your script. I highly recommend that you install the firebug extension if you haven't already -- It's an invaluable tool for messing with Javascript.

  8. #8
    SitePoint Zealot nicc9's Avatar
    Join Date
    Jan 2005
    Location
    New Orleans, LA
    Posts
    181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, all!

    I guess I did, have some minor error in the code and that made me think one couldn't pass an array as an argument.

    once again, thanks for your help.

    n.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •