开发者

is it possible to pass parameters to an assoc array?

think about an array like this:

...
"key1" => some_call("val1", $params),
"key2" => some_call("val1", $params),
...

now i want to pass parameters ($params) when addressing my array entries

$x = $array['key1'] , $params ... 
开发者_JS百科

is there some way to do something like this?

UPDATE

why would i like to do this? i am using codeigniter and in the language file is an assoc array, where the right side holds the text in its predicted language. i want to abuse this a little bit and want to load email templates, where i pass a parameter which holds the values which shell be replaced in the template.

UPDATE 2

for php 5.2.*


Since PHP 5.3 you can use anonymous functions. Maybe you want something like this:

<?php

function some_call($arg,$params)
{
     echo "$arg: ",count($params),"\n";
}

$array = array(
    'key1' => function($params) { some_call('val1',$params); },
    'key2' => function($params) { some_call('val1',$params); }
);

$array['key1'](array(1,2,3));


Instead of anonymous functions (i.e. if you're using PHP5 < 5.3) then you could use the create_function() function to achieve what you want:

function some_call($arg, $params)
{
     echo $arg, ': ', count($params), "\n";
}

$array = array(
    'key1' => create_function('$params', 'some_call("val1", $params);'),
    'key2' => create_function('$params', 'some_call("val2", $params);'),
);

$array['key1'](array(1,2,3));


Make $x an array?

$x[] = $array['key1'] , $params ... 

or

$x = array($array['key1'] , $params ... )

or a concatenated string

$x = $array['key1'] . $params ... // use the . to concat
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜