多维数组拆分与爆炸PHP

时间:2021-02-01 22:04:46

i have multidimension array

我有多维数组

$a = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";

then i explode $a

然后我爆炸$ a

$array_a = explode("|", $a);

//loop

for($i=0;$i<=count($arr_a)-1;$i++){

    arr_b = explode(",", $arr_a[$i]);

    foreach($arr_b as $id => $access){
        echo $id." have access ".$access;
    }
}

//result 
0 have access 5
1 have access add
0 have access 4
1 have access edit
0 have access 6
1 have access add
0 have access 6
1 have access edit
0 have access 19
1 have access add
0 have access 19
1 have access delete
0 have access 19
1 have access view
//-end result

the problem is :

问题是 :

how i can make the result like this

我怎么能这样做的结果

5 have access add
4 have access edit
6 have access add,edit
19 have access add,delete,view

5有访问权限添加4有访问权限编辑6有访问权限添加,编辑19有访问权限添加,删除,查看

5 个解决方案

#1


1  

Try this one (tested):

试试这个(测试过):

// intial data
$strAccess = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";

// build access array
$arrayAccess = array();

$tmpList = explode('|', $strAccess);

foreach($tmpList as $access)
{
  list($idUser, $right) = explode(',', $access);

  if (!isset($arrayAccess[$idUser]))
    $arrayAccess[$idUser] = array();

  $arrayAccess[$idUser][] = $right;
}

// print it out
foreach($arrayAccess as $idUser => $accessList)
  echo $idUser." has access ".implode(",", $accessList)."\n";

#2


2  

I think Regex would be a better solution in this case, in case you haven't considered using it.

在这种情况下,我认为正则表达式会是一个更好的解决方案,以防你没有考虑使用它。

Sample (Tested):

$a = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";

$result = array();

// match each pair in the input
if (preg_match_all('/(\\d+)\\,(.*?)(\\||$)/m', $a, $matches)){

    foreach( $matches[1] as $match_index => $match ){

        $result[$match][] = $matches[2][$match_index];

    }

}

// loop through the results and print in desired format
foreach( $result as $entry_index => $entry ){
    echo "$entry_index have access " . implode( ',', $entry ) . "\n";
}

output:

5 have access add
4 have access edit
6 have access add,edit
19 have access add,delete,view

Using regex simplifies the code and also makes it somewhat easier to change the format of the input that is supported.

使用正则表达式可以简化代码,还可以更轻松地更改支持的输入格式。

The $result array ended up with a hash map using your numeric ids as the keys and arrays of permissions (add, delete, etc) as the value for each key.

$ result数组以哈希映射结束,使用您的数字ID作为键和权限数组(添加,删除等)作为每个键的值。

#3


0  

You can use counter something like

你可以使用类似的计数器

  $count5=0;
  foreach($arr_b as $id => $access){
    if($access=='5'){
    $count5++;
    }
  echo $id." have access ".$count5;
 }

#4


0  

$a = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";
$array_a = explode("|". $a);

//loop
$aRights = array();
for($i=0;$i<=count($arr_a)-1;$i++){ $arr_b = explode(",", $arr_a[$i]);

     foreach($arr_b as $id => $access){
        $aRights[$id][] = $access;
     }
}

foreach( $aRights as $id=>$rightsList){
    echo $id." have access ";
    $i=1;
    foreach($rightsList as $r){

       echo $r;
       if($i != count($rightsList)) echo ",";
       $i++;
   }
}

#5


0  

What about something like hashmapping:

像hashmapping这样的东西:

$tobegenerated = array();
$a = explode(yourdelimiter,yourstring);
foreach ($a as $cur)
{
    $b = explode(delimiter,$cur);
    $tobegenerated[$cur[0]] = $cur[1];
}
//Print hashmap $tobegenerated

This should work, remember that in php arrays are also hash maps

这应该工作,记住在php数组中也是哈希映射

Edit 1: Arrays in PHP are hash maps. A hash map can be interpreted as an array where keys can be anything and value can be anything too. In this case, you would like to use a hash map because you need to bind values that are "outside" the range of the array (even if they are ints). So a hash map is exactly what you want, allowing to specify anything as a key.

编辑1:PHP中的数组是哈希映射。哈希映射可以解释为一个数组,其中键可以是任何值,值也可以是任何值。在这种情况下,您希望使用哈希映射,因为您需要绑定“超出”数组范围的值(即使它们是整数)。因此,哈希映射正是您想要的,允许将任何内容指定为键。

How a hash map work is something that you can search on google or on wikipedia. How arrays work in php can be found here

哈希地图如何工作,您可以在谷歌或*上搜索。如何在PHP中使用数组工作

#1


1  

Try this one (tested):

试试这个(测试过):

// intial data
$strAccess = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";

// build access array
$arrayAccess = array();

$tmpList = explode('|', $strAccess);

foreach($tmpList as $access)
{
  list($idUser, $right) = explode(',', $access);

  if (!isset($arrayAccess[$idUser]))
    $arrayAccess[$idUser] = array();

  $arrayAccess[$idUser][] = $right;
}

// print it out
foreach($arrayAccess as $idUser => $accessList)
  echo $idUser." has access ".implode(",", $accessList)."\n";

#2


2  

I think Regex would be a better solution in this case, in case you haven't considered using it.

在这种情况下,我认为正则表达式会是一个更好的解决方案,以防你没有考虑使用它。

Sample (Tested):

$a = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";

$result = array();

// match each pair in the input
if (preg_match_all('/(\\d+)\\,(.*?)(\\||$)/m', $a, $matches)){

    foreach( $matches[1] as $match_index => $match ){

        $result[$match][] = $matches[2][$match_index];

    }

}

// loop through the results and print in desired format
foreach( $result as $entry_index => $entry ){
    echo "$entry_index have access " . implode( ',', $entry ) . "\n";
}

output:

5 have access add
4 have access edit
6 have access add,edit
19 have access add,delete,view

Using regex simplifies the code and also makes it somewhat easier to change the format of the input that is supported.

使用正则表达式可以简化代码,还可以更轻松地更改支持的输入格式。

The $result array ended up with a hash map using your numeric ids as the keys and arrays of permissions (add, delete, etc) as the value for each key.

$ result数组以哈希映射结束,使用您的数字ID作为键和权限数组(添加,删除等)作为每个键的值。

#3


0  

You can use counter something like

你可以使用类似的计数器

  $count5=0;
  foreach($arr_b as $id => $access){
    if($access=='5'){
    $count5++;
    }
  echo $id." have access ".$count5;
 }

#4


0  

$a = "5,add|4,edit|6,add|6,edit|19,add|19,delete|19,view";
$array_a = explode("|". $a);

//loop
$aRights = array();
for($i=0;$i<=count($arr_a)-1;$i++){ $arr_b = explode(",", $arr_a[$i]);

     foreach($arr_b as $id => $access){
        $aRights[$id][] = $access;
     }
}

foreach( $aRights as $id=>$rightsList){
    echo $id." have access ";
    $i=1;
    foreach($rightsList as $r){

       echo $r;
       if($i != count($rightsList)) echo ",";
       $i++;
   }
}

#5


0  

What about something like hashmapping:

像hashmapping这样的东西:

$tobegenerated = array();
$a = explode(yourdelimiter,yourstring);
foreach ($a as $cur)
{
    $b = explode(delimiter,$cur);
    $tobegenerated[$cur[0]] = $cur[1];
}
//Print hashmap $tobegenerated

This should work, remember that in php arrays are also hash maps

这应该工作,记住在php数组中也是哈希映射

Edit 1: Arrays in PHP are hash maps. A hash map can be interpreted as an array where keys can be anything and value can be anything too. In this case, you would like to use a hash map because you need to bind values that are "outside" the range of the array (even if they are ints). So a hash map is exactly what you want, allowing to specify anything as a key.

编辑1:PHP中的数组是哈希映射。哈希映射可以解释为一个数组,其中键可以是任何值,值也可以是任何值。在这种情况下,您希望使用哈希映射,因为您需要绑定“超出”数组范围的值(即使它们是整数)。因此,哈希映射正是您想要的,允许将任何内容指定为键。

How a hash map work is something that you can search on google or on wikipedia. How arrays work in php can be found here

哈希地图如何工作,您可以在谷歌或*上搜索。如何在PHP中使用数组工作