PHP:用变量填充由键定义的数组的特定值

发布于 2024-12-17 19:28:42 字数 780 浏览 1 评论 0 原文

我正在尝试填充数组的特定值,该值由带有变量的键定义。 该值保存语法的形式,键是它的标识符。

我不确定这是否是正确的方法,但这就是我遇到麻烦的地方:

function create_mess($data){
    echo message_list($data);   
    }

function message_list($data){
    $messages = array(
        1000 => $data['user']." logged on!",
        2000 => $data['user']." comment on ".$data['user_rep']."s comment about!".$data['title'],
        2010 => "You just received a reply to".$data['title']."by ".$data['user'],
        3000 => $data['user'].": ".$data['title']."!"
    );

    return $messages[$data['mess_id']];
    }

这是我所拥有的内容的精简版本,以便使事情更加清晰。我知道 create_mess 函数目前并不是一个非常有用的函数。

目前它总是填充所有变量。我想知道的是:

  • 是否可以只填写通过函数传递的标识符键的变量?
  • 这可以用数组实现吗?
  • 或者只有这些句子存储在数据库中?

I'm trying to populate a specific value of an array, defined by a key with, variables.
The value holds a form for a syntax, the key is the identifier for it.

I'm not sure if this is the right way to this, but it's where I am having trouble:

function create_mess($data){
    echo message_list($data);   
    }

function message_list($data){
    $messages = array(
        1000 => $data['user']." logged on!",
        2000 => $data['user']." comment on ".$data['user_rep']."s comment about!".$data['title'],
        2010 => "You just received a reply to".$data['title']."by ".$data['user'],
        3000 => $data['user'].": ".$data['title']."!"
    );

    return $messages[$data['mess_id']];
    }

This is a trimmed down version of what I have in order to make things more clear. I'm aware that the create_mess function isn't a very useful one right now.

Currently it always fills in all the variables. What I want to know is:

  • Is it possible to only fill in the variables for the identifier key you pass through the function?
  • Is this possible with an array?
  • Or only if these sentences are stored in a DB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月牙弯弯 2024-12-24 19:28:42
    foreach($messages as $key => &$message)
        if($key != $data['mess_id'])
            unset($messages[$data['mess_id']];

这只会在填充所有值后删除数组(效率低下)。如果您想有条件地填充数组,则必须围绕创建要根据消息 ID 返回的数组构建一些逻辑。例如:将当前的消息数组视为“模板”来构建您将返回的辅助数组。

    foreach($messages as $key => &$message)
        if($key != $data['mess_id'])
            unset($messages[$data['mess_id']];

this will just strip down the array after having filled in all the values (inefficient). If you want to conditionally fill the array, you'll have to build some logic around the creation of the array you want to return based on the message id. For example: treat your current messages array as a "template" to build a secondary array you will return.

萤火眠眠 2024-12-24 19:28:42

如果我没理解错的话,是的。您可以将参数声明为空:
function create_mess($key1=null, $key2=null) { etc... }

您可以将其作为数组传递,然后使用 列表
函数create_mess($array) { 列表($key1, $key2)=$array; 如果这不是您想要的,那么

您必须更具体一些。

If I'm understanding you correctly, yes. You can declare the arguments as null:
function create_mess($key1=null, $key2=null) { etc... }

You can pass it as an array then use list:
function create_mess($array) { list($key1, $key2)=$array; }

If this isn't what you're looking for, you'll have to be a little more specific.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文