7 Replies - 13402 Views - Last Post: 04 August 2011 - 12:15 PM Rate Topic: -----

#1 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

making multidimensional array tossed to a function?

Posted 02 August 2011 - 03:51 PM

Hi, I am trying to understand how I can send a multidimensional array to a function as a parameter.

How would you do this? and in the function how would you grab the array within the array.
Is This A Good Question/Topic? 0
  • +

Replies To: making multidimensional array tossed to a function?

#2 maniacalsounds   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 474
  • Joined: 27-June 10

Re: making multidimensional array tossed to a function?

Posted 02 August 2011 - 04:48 PM

You'd do it just like any other argument. Put it in parentheses in the function declaration. Be sure to use the array_keys() function:
function functionHere($3darray) {
     $keys = array_keys($3darray);
     foreach ($3darray[$keys[0]] as $keycounter => $value) {
          echo $value;
     }
}


Granted, change it to your requirements.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3796
  • View blog
  • Posts: 13,742
  • Joined: 08-August 08

Re: making multidimensional array tossed to a function?

Posted 02 August 2011 - 05:58 PM

I'm not positive how PHP handles passing arrays, but I think passing an array by reference might be more efficient:

function foo(&$some_array) {
  // do something with $some_array
}
$myarray = array(array(1,2),array(3,4));
foo($myarray);


Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: making multidimensional array tossed to a function?

Posted 02 August 2011 - 10:51 PM

View PostCTphpnwb, on 03 August 2011 - 02:58 AM, said:

I'm not positive how PHP handles passing arrays, but I think passing an array by reference might be more efficient:

that depends on what you want to do with the array. if they are just key-value pairs for setting some object or similar, referencing might not be necessary (i.e. when you do $pdostatement->execute(array("key" => "value")) in a function)
Was This Post Helpful? 0
  • +
  • -

#5 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: making multidimensional array tossed to a function?

Posted 04 August 2011 - 01:57 AM

ok, here is what I need to do.

I need to generate an array in php then toss it at javascript and the javascript needs to generate a drop down menu to list the php array of data.

how can I do this?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: making multidimensional array tossed to a function?

Posted 04 August 2011 - 02:09 AM

why not generating the dropdown menu in PHP and only insert it through JS?
Was This Post Helpful? 0
  • +
  • -

#7 hockey97   User is offline

  • D.I.C Regular

Reputation: -77
  • View blog
  • Posts: 300
  • Joined: 25-September 08

Re: making multidimensional array tossed to a function?

Posted 04 August 2011 - 11:55 AM

View PostDormilich, on 04 August 2011 - 03:09 AM, said:

why not generating the dropdown menu in PHP and only insert it through JS?



well, I am in the hosting business. I need 2 things generated in javascript. it's a input text box and a drop down menu.

the text box will be where the user will input their domain name of their choice.

The drop down menu is where the TLDs names are at you know the .com , .net etc.

I want the user to be able to order more then one at the given time.

so how would I pass the php array to javascript?
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4208
  • View blog
  • Posts: 13,283
  • Joined: 08-June 10

Re: making multidimensional array tossed to a function?

Posted 04 August 2011 - 12:15 PM

View Posthockey97, on 04 August 2011 - 08:55 PM, said:

so how would I pass the php array to javascript?

you could encode it in JSON. you can as well return the full dropdown code for JS to insert

file.php
<?php
echo <<<HTML
<select name="tld">
    <option>.com</option>
    <option>.net</option>
    <option>.org</option>
</select>
HTML;

file.html
<!-- html code -->
<div id="dropdown"></div>
<!-- more html code -->

file.js
//# simplified
var xhr = new XMLHttprequest;
xhr.open("GET", "file.php", true);
xhr.onreadystatechange = function()
{
    if (4 == this.readyState && 200 == this.status) {
        document.getElementById("dropdown").innerHTML = this.responseText;
    }
}
xhr.send();

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1