n1concepts Posted September 28, 2010 Share Posted September 28, 2010 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); } Link to comment https://forums.phpfreaks.com/topic/214660-how-do-i-pass-arguements-into-function-set-variables-inside/ Share on other sites More sharing options...
trq Posted September 28, 2010 Share Posted September 28, 2010 function all_products($name, $price, $shipping) { Link to comment https://forums.phpfreaks.com/topic/214660-how-do-i-pass-arguements-into-function-set-variables-inside/#findComment-1116931 Share on other sites More sharing options...
n1concepts Posted September 29, 2010 Author Share Posted September 29, 2010 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! Link to comment https://forums.phpfreaks.com/topic/214660-how-do-i-pass-arguements-into-function-set-variables-inside/#findComment-1116937 Share on other sites More sharing options...
trq Posted September 29, 2010 Share Posted September 29, 2010 Functions except arguments, these arguments then show up within your function as the variables you declared within your argument list. eg; function foo($bar) { echo $bar; } foo('hello'); Link to comment https://forums.phpfreaks.com/topic/214660-how-do-i-pass-arguements-into-function-set-variables-inside/#findComment-1116946 Share on other sites More sharing options...
n1concepts Posted September 29, 2010 Author Share Posted September 29, 2010 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 https://forums.phpfreaks.com/topic/214660-how-do-i-pass-arguements-into-function-set-variables-inside/#findComment-1116972 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.