JustinB Posted April 13, 2008 Share Posted April 13, 2008 i am trying to pass an array to a function then echo it this is driving me nutty i beleve this is an associative array not sure thow array('d' => 'a', 'f' => 'g', 'h' => 'c') this is what i have that doesnt work any help whold be aprischiated <?php function assign($var_array){ foreach ($var_array AS $var => $content){ echo $var $content; } } assign( array('d' => 'a', 'f' => 'g', 'h' => 'c')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/ Share on other sites More sharing options...
marcus Posted April 13, 2008 Share Posted April 13, 2008 i get "dafghc" as my result (separate the variables with a period) <?php function assign($var_array) { foreach ($var_array as $var => $content) { echo $var . $content; } } assign(array('d' => 'a', 'f' => 'g', 'h' => 'c')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515873 Share on other sites More sharing options...
JustinB Posted April 13, 2008 Author Share Posted April 13, 2008 i get "dafghc" as my result (separate the variables with a period) <?php function assign($var_array) { foreach ($var_array as $var => $content) { echo $var . $content; } } assign(array('d' => 'a', 'f' => 'g', 'h' => 'c')); ?> that fixed it but can you explain why thous variables need to be seperated with a period it doesnt seem like it whould be needed please forgive me i am quite new Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515878 Share on other sites More sharing options...
kenrbnsn Posted April 13, 2008 Share Posted April 13, 2008 The period is the concatenation operator. You can not say <?php $a = "this is"; $b = "a string"; echo $a $b; ?> You can do <?php $a = "this is"; $b = "a string"; echo $a . $b; ?> or <?php $a = "this is"; $b = "a string"; echo "$a $b"; ?> or <?php $a = "this is"; $b = "a string"; echo $a . ' ' . $b; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515885 Share on other sites More sharing options...
JustinB Posted April 13, 2008 Author Share Posted April 13, 2008 thanks for your help i have another question not sure if i should start a new post or not is it possible to pass a loop to another function to print it maybe my example will help <?php class test { var $testvar; function assign($var_array,$cac) { foreach ($var_array AS $var => $content) { $this->testvar = $var . $content; } } function output() { echo $this->testvar; } } $tpl = new test(); $tpl->assign(array('d' => 'a', 'f' => 'g', 'h' => 'c')," mmmmn"); $tpl->output(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515924 Share on other sites More sharing options...
Barand Posted April 13, 2008 Share Posted April 13, 2008 pass a loop ??? ??? Have you tried your code? Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515946 Share on other sites More sharing options...
JustinB Posted April 13, 2008 Author Share Posted April 13, 2008 pass a loop ??? ??? Have you tried your code? yes it only prints out one value not all them Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515950 Share on other sites More sharing options...
Barand Posted April 13, 2008 Share Posted April 13, 2008 change $this->testvar = $var . $content; to $this->testvar .= $var . $content; Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515951 Share on other sites More sharing options...
JustinB Posted April 13, 2008 Author Share Posted April 13, 2008 change $this->testvar = $var . $content; to $this->testvar .= $var . $content; thanks for your help thous period's are tricky Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-515958 Share on other sites More sharing options...
doni49 Posted April 13, 2008 Share Posted April 13, 2008 http://www.php.net/manual/en/language.operators.string.php String Operators There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information. <?php $a = "Hello "; $b = $a . "World!"; // now $b contains "Hello World!" $a = "Hello "; $a .= "World!"; // now $a contains "Hello World!" ?> Quote Link to comment https://forums.phpfreaks.com/topic/100873-how-do-i-pass-an-associative-array-to-a-function/#findComment-516019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.