Samuel
Samuel

Reputation: 781

combine strings into an array in php

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.

Upvotes: 1

Views: 3472

Answers (7)

Alex
Alex

Reputation: 7688

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

Upvotes: 0

Baba
Baba

Reputation: 95111

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

Upvotes: 5

NLZ
NLZ

Reputation: 945

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.

Upvotes: 0

Jayram
Jayram

Reputation: 19578

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

Upvotes: 0

Carsten
Carsten

Reputation: 18446

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

Upvotes: 1

Dale
Dale

Reputation: 10469

Something along the lines of this should help:

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

Upvotes: 1

netiul
netiul

Reputation: 2769

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

Upvotes: 1

Related Questions