1

我有一个 Web 服务,可以从外部数据库中识别人员及其功能,如果登录成功,该数据库会返回一组数据。数据(我现在感兴趣)以不同的字符串分隔,如下所示:

$groups="group1, group2, group3"
$functions="member, member, admin"

字符串$groups的第一个元素对应于字符串的第一个元素$functions

我们可以在字符串中有空点:

$groups="group1,, group3";
$functions="member,, admin";

将它们结合起来获得的最佳方法是什么:

$usertype(
    group1=>member, 
    group2=>member, 
    group3=>admin,
);

然后我打算用来array_search()获取对应一个函数的组名。

4

7 回答 7

4

这是一个非常技巧,尤其是当第一个元素为空时,但这是一个全面的解决方案

你需要的是:

// Your Varriables
$groups = "group1,, group3";
$functions = "member,, admin";

// Break Into Array
$groups = explode(",", $groups);
$functions = explode(",", $functions);

// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);

如果您需要修复null值,则:

例子 :

// Your Varriables
$groups = "group1,, group3";
$functions = "member,, admin";

// Break Into Array
$groups = explode(",", $groups);
$functions = explode(",", $functions);

// Fix Null Values
$groups = fixNull($groups, true);
$functions = fixNull($functions);

// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);

输出

Array
(
    [group1] => member
    [group2] => member
    [group3] => admin
)

观看现场演示

更复杂:

// Your Varriables
$groups = ",,, group3";
$functions = ",member,, admin";

// Fix Null Values
$groups = fixNull(explode(",", $groups), true);
$functions = fixNull(explode(",", $functions));

// Combine both new Arrays and Output Result
$new = array_combine($groups, $functions);
print_r($new);

输出

Array
(
    [group4] => member
    [group5] => member
    [group6] => member
    [group3] => admin
)

现场演示

使用的功能

function fixNull($array, $inc = false) {
    $ci = new CachingIterator(new ArrayIterator($array), CachingIterator::FULL_CACHE);
    if ($inc) {
        $next = array_filter($array);
        $next = current($next);
        $next ++;
    } else {
        $next = array_filter($array);
        sort($next);
        $next = end($next);
    }

    $next || $next = null;
    $modified = array();

    foreach($ci as $item) {
        $modified[] = empty($item) ? trim($next) : trim($item);
        if (! $ci->getInnerIterator()->current()) {
            $item || $item = $next;
            $next = $inc ? ++ $item : $item;
        }
    }
    return $modified;
}
于 2013-06-11T12:22:47.080 回答
1
$groups = explode(",", $groups);
$functions = explode(",", $functions);

//then use the elements of the $groups array as key and the elements of the $functions array as the value
$merged = array_combine($groups, $functions);
于 2013-06-11T12:17:24.873 回答
1

类似的东西应该会有所帮助:

$usertype = array_combine(explode(',', $groups), explode(',', $functions));
于 2013-06-11T12:17:54.050 回答
1

用于explode()制作字符串数组,并将array_combine()一个数组用作键,另一个用作值。

$groups = "group1, group2, group3";
$functions = "member, member, admin";

$usertype = array_combine(explode(", ", $groups), explode(", ", $functions));
于 2013-06-11T12:19:07.533 回答
0

您是否尝试过然后过滤数组?我认为这将是一个很好的方法:explode($delimiter , $string)

$groups="group1,, group3";
$functions="member,, admin";

$groups_array = array_map('trim',explode(',', $groups));
$functions_array = array_map('trim',explode(',', $functions));

$final = array();
for ($i = 0; $i <= count($groups_array); $i++) {
    $final[$groups_array[$i]] = $functions_array[$i];
}

$merged = array_combine($groups_array, $functions_array);

演示

于 2013-06-11T12:15:53.917 回答
0

爆炸和array_combine。请注意,您遇到了空格问题。因此使用以下内容:

<?php
$groups="group1, group2, group3";
$functions="member, member, admin";

$groupArray = array_map(function($element){return trim($element);},      
explode(',',$groups)); // split into array and remove leading and ending whitespaces
$functionArray = array_map(function($element){return trim($element);},   
explode(',',$functions));  // split into array and remove leading and ending whitespaces

print_r(array_combine($groupArray,$functionArray));
?>

这将输出:

Array
(
    [group1] => member
    [group2] => member
    [group3] => admin
)

编辑:删除了第一个元素为空的可能性的修剪。

于 2013-06-11T12:20:08.777 回答
0
$groups="group1, group2, group3"
$functions="member, member, admin"

preg_match_all('/\w+/', $groups, $groups);
preg_match_all('/\d+/', $functions, $functions);

$final = array();
foreach ($groups[0] AS $key => $letter)
    $final[] = $letter . '=>' . $functions[0][$key];

echo join(', ', $final);
于 2013-06-11T12:20:11.703 回答