Jump to content

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


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


Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • 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.