SiC_Goat Posted October 15, 2008 Share Posted October 15, 2008 I'm aware that array is a language construct and not a function, however I'm curious to the extend of the T_DOUBLE_ARROW token [read: =>] and it's usability in user functions. Is it possible to define a function that accepts 'k => v' key pair sets in the same manner of array? I'm trying to avoid having to 'foo ( array ( bar1 => bar2, bar3 => bar4 ) )' every time I want to call this specific function. Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/ Share on other sites More sharing options...
dennismonsewicz Posted October 15, 2008 Share Posted October 15, 2008 you could possibly use a foreach loop http://us3.php.net/manual/en/control-structures.foreach.php Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-666086 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2008 Share Posted October 15, 2008 Can you give us a hint as to what the function is supposed to look like? Code? If we see what you're trying to do, perhaps we can help you better. Ken Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-666091 Share on other sites More sharing options...
SiC_Goat Posted October 20, 2008 Author Share Posted October 20, 2008 I want to do something like function demoStub ( KeyPair $pair ) { foreach ( $pair as $k => $v ) { // magic } } demoStub ( "red" => "fish", "blue" => "fish", "one" => "fish", "two" => "fish" ); I'm fairly certain at this point that it is impossible to construct such a function as array is a language structure rather than a function type. Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-669991 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 wrap the word array around your key/pairs like so: <?php function demoStub ( $pairs ) { foreach ( $pairs as $k => $v ) { print "$k: $v<br />"; } } demoStub ( array("red" => "fish", "blue" => "fish", "one" => "fish", "two" => "fish") ); ?> Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-670000 Share on other sites More sharing options...
SiC_Goat Posted October 20, 2008 Author Share Posted October 20, 2008 wrap the word array around your key/pairs like so: That is exactly what I was trying to work around. It just glares at me with dirty eyes Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-670167 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 why are you trying to avoid it? Link to comment https://forums.phpfreaks.com/topic/128530-solved-passing-key-pairs-into-a-function/#findComment-670170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.