1

I have a web service to identify people and their functions from an external database that returns me a set of data if the login is successful. The data (that interests me right now) is separated in different strings as follow:

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

The first element of the string $groups corresponds to the first element of the $functions string.

We can have empty spots in the strings:

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

What is the best way to combine them to obtain:

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

Then I plan to use array_search() to get the name of the group that corresponds to a function.

1
  • Should the strings with empty spots still yield the same end result? Or is it just an example of what empty spots look like, i.e. the end result would only contain group1 and group3? Commented Jun 11, 2013 at 13:25

7 Answers 7

5

This is very trick especially when the first element is empty but here is a comprehensive solution

What you need is :

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

If you need to fix null values then :

Example :

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

Output

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

See Live DEMO

More Complex:

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

Output

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

Live DEMO

Function Used

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Something along the lines of this should help:

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

Comments

1

Use explode() to make arrays of your strings, and array_combine() to use one array as keys, the other one as values.

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

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

Comments

0

Have you tried a explode($delimiter , $string) and then filter the array? I think that would be a good way of doing it:

$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

3 Comments

yes but then I get to separate arrays. The filter part, I do not understand.
thank you. I am checking it out. Thank you again for your time.
you are very welcome, and an upvote (if note answer accepted) would do for the time spent :)
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);

Comments

0

explode and array_combine. Note that you have a problem with whitespaces. Therefore use the following:

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

This will output:

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

EDIT: removed trim for the possibility that the first element is empty.

2 Comments

Why did you change the variables ???? OP said group1,, group3 not group1, group2, group3
That is OP's second example output, both options will work with my example code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.