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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

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