在php中,将字符串组合成数组


combine strings into an array in php

我有一个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()来获取对应于一个函数的组的名称。

当第一个元素为空时,这是一个非常棘手的问题,但这里有一个全面的解决方案

你需要的是:

// 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
)

参见Live DEMO

更复杂:

// 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;
}
$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);

类似的内容应该会有所帮助:

$usertype = array_combine(explode(',', $groups), explode(',', $functions));

使用explode()来创建字符串数组,使用array_combine()来使用一个数组作为键,另一个作为值。

$groups = "group1, group2, group3";
$functions = "member, member, admin";
$usertype = array_combine(explode(", ", $groups), explode(", ", $functions));

您是否尝试过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);

$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);

explosion and 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
)

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