将电子表格转换为数组并循环并调用函数

发布于 2024-10-09 03:28:00 字数 366 浏览 0 评论 0原文

这与在 BuddyPress 中生成组有关。

我有一个电子表格,其中包含(在本例中)组名称、组描述和别名。

我需要从文件中获取信息,将其转换为数组,然后循环遍历它并每次调用 groups_create_group() 。

我可以在 bp-groups.php (http://www.nomorepasting.com/getpaste.php?pasteid=35217) 中找到该函数。它告诉我您需要填写的所有参数。

我对此很陌生,正在寻找如何做到这一点。你知道我如何获取这些信息并将其转换为数组吗?循环遍历并每次调用 groups_create_group() ?

如果您也有任何与此相关的方便链接,我将不胜感激。

This is related to generate groups in BuddyPress.

I have a spreadsheet with (in this case) a group name, group description and slug.

I need to grab the information from the file, turn it into an array, then loop through it and call groups_create_group() every time.

I can find that function in bp-groups.php (http://www.nomorepasting.com/getpaste.php?pasteid=35217). It tells me all the parameters you need to fill in.

I'm quite new to this and looking for how I can do this. Do you know how I can grab this information and turn it into an array? An loop it through and call groups_create_group() every time?

If you have any handy links regarding this as well I'll appreciate it.

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

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

发布评论

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

评论(1

你好,陌生人 2024-10-16 03:28:00

只要您具有可用的 groups_create_group 函数(即已包含所需的文件),您应该能够执行类似以下

<?php

$groups = array();

if (($handle = fopen("groupData.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $group = array('group_id' => 'SOME ID', 'name' => $data[0], 'description' => $data[1], 'slug' => $data[2], 'date_created' => gmdate( "Y-m-d H:i:s" ), 'status' => 'public' );
        $groups[] = $group;
    }   
    fclose($handle);
}

foreach ($groups as $group) {
    groups_create_group($group);
} 

操作注意,您提供的粘贴代码中对 groups_create_group 的调用是显式调用另一个方法来清理之前的 slug将其传递给函数。因此,您可能需要将对 $group 变量的赋值更改为:

 $group = array('group_id'    => 'SOME ID', 
                'name'        => $data[0], 
                'description' => $data[1], 
                'slug' => groups_check_slug(sanitize_title(esc_attr($data[2]))), 
                'date_created' => gmdate( "Y-m-d H:i:s" ), 
                'status' => 'public' 
 );

As long as you have the function groups_create_group available (ie. The required file has been included) you should be able to do something like this

<?php

$groups = array();

if (($handle = fopen("groupData.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $group = array('group_id' => 'SOME ID', 'name' => $data[0], 'description' => $data[1], 'slug' => $data[2], 'date_created' => gmdate( "Y-m-d H:i:s" ), 'status' => 'public' );
        $groups[] = $group;
    }   
    fclose($handle);
}

foreach ($groups as $group) {
    groups_create_group($group);
} 

Note, the call to groups_create_group in the pasted code you provided was explicitly calling another method to sanitize the slug before passing it to the function. So you may want to change the assignment to the $group variable to this:

 $group = array('group_id'    => 'SOME ID', 
                'name'        => $data[0], 
                'description' => $data[1], 
                'slug' => groups_check_slug(sanitize_title(esc_attr($data[2]))), 
                'date_created' => gmdate( "Y-m-d H:i:s" ), 
                'status' => 'public' 
 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文