在php中将字符串组合成一个数组

时间:2013-06-11 12:13:25

标签: php arrays string

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

7 个答案:

答案 0 :(得分: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
)

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

Live DEMO

使用的功能

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;
}

答案 1 :(得分: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);

答案 2 :(得分:1)

有些事情应该有所帮助:

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

答案 3 :(得分:1)

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

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

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

答案 4 :(得分: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);

Demo

答案 5 :(得分:0)

explode和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
)

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

答案 6 :(得分: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);