如何用键和值爆炸阵列多维度

时间:2021-09-30 22:08:25

I have string like this:

我有这样的字符串:

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

What I've done:

我所做的:

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

Then will show it, like:

然后显示它,比如:

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

But I want not like that. I want make it to be array mutlidimentional with key and value, like this:

但我不想那样。我想让它是数组mutlidional带有key和value,如下所示:

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

Then, How to show it's in table tag html like this?

那么,如何在表标记html中显示它呢?

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

thank you for your attention
NB: You can change the separator in string if it's difficult with separator like that

谢谢您的关注NB:如果使用这样的分隔符有困难,您可以在字符串中更改分隔符

5 个解决方案

#1


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

#2


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

#3


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

#4


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

#5


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
)

#1


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

#2


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

#3


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

#4


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

#5


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
)