Jump to content

How do I pass arguements into function & set variables inside?


n1concepts

Recommended Posts

I have the array defined and print_r (outside the function is show what I need...)

However, I need to somewhay pass those three arguments - name, price, shipping (keys & values) into the "all_products" function and assign (inside the function) each element to a variable {name, price, shipping}.

 

function all_products($key,$value) {

# This is where I'm lost - I know the values are passed to the function but how to I assign them to below variable?
echo "$name";
echo "$price";
echo "$shipping";


// Products array defined to send values into "all_products" function & loop to display item name & individual pricing (using WHILE LOOP)
$products = array('Product1' =>
                                    array   (   'name'  =>  'item one',
                                                'price'	=> '30.00',
				                            'shipping'	=> '0.00'),
             'Product2' =>                       
		                        array 	(   'item two',
                                                'price'	=> '19.50',
				                            'shipping'	=> '0.10'),
             'Product3' =>
		                        array	(   'item three',
                                                'price'	=> '21.99',
				                            'shipping'	=> '0.10')
                                                );


while (list($keys, $values) = each($products)) {
             while (list($k, $v) = each($values)) 
   echo "Checking Looped Data Outside Function: <strong>$k: </strong> $v<br/>";

#   echo "<pre>";
#   print_r($value);
#   echo "</pre>";

# Trying to send array elements into "all_products" function & echo (name, price, shipping) keys and values inside that function
all_products($k,$v);

}

I tried that but unable to see the entries?

 

I even configured with references then global to which I got the last (3D) to display which meant I wasn't saving the 1st two demensions some how...

 

Anyway, I'm still playing around with it - thx!

Thanks - I understand.

 

The problem with the script not working was I had the function call outside of the "While" loop that was traversiing the array - defining the elements.

 

Once I moved acouple of lines (the variable calling that function inside the brackets, it worked like a charm).

 

Thanks for replies!

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.