Jump to content

[SOLVED] passing multilevel array to function, output


benjaminbeazy

Recommended Posts

i am still newb at functions and classes

 

im trying to pass a multilevel array to a function of a class, such as this..

 

$array1 = array("w" => "value", "x" => "value2");

$array2 = array("y" => "value3", "z" => "value4");

$search = new Search();

$search->query($array1, $array2);

 

is that the correct way to pass an array, and how do i access the arrays, contents, and original key names (w,x,y,z) within the function

 

currently i have something like this for my class/function

 

class Search{
  function query(){
    $args = func_get_args();
    foreach($args as $var => $val){
      echo "$var = $val";
    }
  }
}

 

and that gives me:

 

0 = Array

1 = Array

 

how do i get it to output:

 

array 1

w = value1

x = value2

array 2

y = value3

z = value4

 

 

??? all help is very much appreciated, p.s. i have access to both php 4 & 5 and have no preference for this

First, those are not multi-dimensioned arrays, each of those are single dimensioned arrays.

Second, when you define a function, you should tell PHP how many parameters to expect, then you can used them as normal variables:

<?php
class Search{
  function query($ary1,$ary2){
     echo '$ary1:<br>';
     foreach ($ary1 as $k => $v)
           echo $k . ' = ' . $v . '<br>';
     echo '$ary2:<br>';
     foreach ($ary2 as $k => $v)
           echo $k . ' = ' . $v . '<br>';
  }
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.