3

I have a string like this:

$string = Leader,Brian; Elder,Nina,Maria; Member,Duke,Rai,Mike

What I've done:

$explode = ("; ", $string);

Then it will show like:

Array ([0] => Leader,Brian [1] => Elder,Nina,Maria [2] => Member,Duke,Rai,Mike)

But I don't like that. I want to make a multidimensional array with key and value, like this:

Array ([Leader] => Brian [Elder] => Array ([0] => Nina [1] => Maria) 
       [Member] => Array ([0] => Duke [1] => Rai [2] => Mike))

How to show it in table tag html like this?

Leader  |  Brian
Elder   |  Nina
        |  Maria
Member  |  Duke
        |  Rai
        |  Mike 

NB: You can change the separator in string if it's difficult with separator like that

Vickel
  • 7,879
  • 6
  • 35
  • 56
Stfvns
  • 1,001
  • 5
  • 16
  • 42

5 Answers5

3

Youre explode is a good first step.

Next one is to iterate its result and explode it again. Then (in the same loop) use first element of the second explode's result as key and the rest as value.

$string = "Leader,Brian; Elder,Nina,Maria; Member,Duke,Rai,Mike";

$exploded = explode("; ", $string);

foreach($exploded as $element) {
    $arr = explode(',', $element);

    $result[array_shift($arr)] = $arr;
}

var_dump($result);

Result:

array(3) {
  ["Leader"]=>
  array(1) {
    [0]=>
    string(5) "Brian"
  }
  ["Elder"]=>
  array(2) {
    [0]=>
    string(4) "Nina"
    [1]=>
    string(5) "Maria"
  }
  ["Member"]=>
  array(3) {
    [0]=>
    string(4) "Duke"
    [1]=>
    string(3) "Rai"
    [2]=>
    string(4) "Mike"
  }
}
Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
1

I would probably walk the array after splitting the string and split again:

$list = explode("; ", $string);
array_walk($list, function(&$element) {
    $names = explode(',', $element);
    // Take the first item off the array type
    $type = array_shift($names);
    // Alter the array in-place to create the sub-array
    $element = [$type => $names]
});
Matt S
  • 14,976
  • 6
  • 57
  • 76
0

You need two "explodes"

$string = 'Leader,Brian; Elder,Nina,Maria; Member,Duke,Rai,Mike';
$explode = explode("; ", $string);
foreach($explode as $row){
    $explode2 = explode(",", $row);
    foreach($explode2 as $i=>$p ){

        echo ( $i > 0 )?"\n\t|":"\n";
        echo $p;
    }
}
cmnardi
  • 1,051
  • 1
  • 13
  • 27
0

You need to explode twice - on your two delimiters, for example:

<?php
$string = "Leader,Brian; Elder,Nina,Maria; Member,Duke,Rai,Mike";

$result = array();
$parts = explode(";", $string);

foreach ($parts as $part)
{
    $kv = explode(",", $part);
    $result[array_shift($kv)] = $kv;
}

// result:
echo '<pre>';
print_r($result);
echo '</pre>';
?>

Will give you:

Array
(
    [Leader] => Brian
    [Elder] => Nina
    [Member] => Duke
)
Stuart
  • 6,630
  • 2
  • 24
  • 40
0

This should do it:

<?php
$string = 'Leader,Brian; Elder,Nina,Maria; Member,Duke,Rai,Mike';

$explode = array_map(function($v){return explode(',', $v);}, explode(';', $string));

$new_array = [];

foreach($explode as $k=>$v)
{
    $v[0] = trim($v[0]);

    $new_array[$v[0]] = $v;

    unset($new_array[$v[0]][0]);

    $new_array[$v[0]] = array_values($new_array[$v[0]]);
}

print_r($new_array);
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77