Jump to content

[SOLVED] Passing key pairs into a function


SiC_Goat

Recommended Posts

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.

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.

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") );
?>

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.