【问题标题】:Multiple input php array in wordpress pluginwordpress插件中的多个输入php数组
【发布时间】:2021-06-01 17:51:52
【问题描述】:

在 wordpress 插件中,我使用数组将元字段导出到 csv。

数组如下所示:

    $export_data_mapping = [
            "meta_1" => "participant_last_name",
            "meta_2" => "participant_first_name",
            "meta_3"=> "participant_birthdate",
            "meta_4" => "participant_current_club",
];

有时“meta_1”的值是空的,将被另一个元字段替换,称为“meta_2”。我需要完成的是,当“meta_1”为空时,将其替换为“meta_2”。

我尝试了以下方法,但没有成功:

$export_data_mapping = [
        "meta_1", "meta_2" => "participant_last_name",
        "meta_2" => "participant_first_name",
        "meta_3"=> "participant_birthdate",
        "meta_4" => "participant_current_club",

];

有人知道答案了吗?

【问题讨论】:

  • 您的问题不清楚,需要更多解释您要达到的目标。编辑它。
  • 你说得对,我现在说得更清楚了。
  • 所以你第一次发布它并因为它已经被回答而被关闭,所以你开始另一个?

标签: php arrays wordpress


【解决方案1】:

这是应该的。

$export_data_mapping = [
    "meta_1" => "",
    "meta_2" => "participant_first_name",
    "meta_3"=> "participant_birthdate",
    "meta_4" => "participant_current_club",
];

foreach ($export_data_mapping as $key => $value) {
    // Check if its empty.
    if ($value == '') {

        // Find which meta_?? is it.
        $meta_number = intval(substr($key, -1));

        // Get the next meta by adding 1
        $new_key = 'meta_' . strval($meta_number + 1);

        // Check if the next meta exist or not, in case the empty meta is last we can't set it.
        if (array_key_exists($new_key, $export_data_mapping)) {
            // Make the metas value as the next metas.
            $export_data_mapping[$key] = $export_data_mapping[$new_key];
        }
    }
}

var_dump($export_data_mapping);

我添加了 cmets 来解释每一行的作用。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2019-08-03
    • 2012-08-25
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多