通过多个分隔符将字符串分解为多维数组php

时间:2022-06-12 21:31:20

Can someone please give me a hand making this explode function recursive? My head is not working today.

有人可以帮我一个手工制作这个爆炸功能递归吗?我的头今天不工作了。

function expl($str,$charlist='|'){
    $charlist = str_split($charlist);
    foreach($charlist as $char){
        if(is_array($str)){
            for($i=0; $i<sizeof($str); $i++){
                $str[$i] = expl($str[$i],$char);
            }
        }else{
            return (explode($char,trim($str,$char)));
        }
    }
    return($str);
}
echo "<pre>";
print_r(expl("A~a1~a2|B~b1~b2",'|~'));
echo "</pre>";

Should output:

Array
(
[0] => Array
    (
        [0] => A
        [1] => a1
        [2] => a2
    )
[0] => Array
    (
        [0] => B
        [1] => b1
        [2] => b2
    )
)

3 个解决方案

#1


1  

<?php
function expl($str,$charlist='|'){
if(!$charlist) return($str);
$char = $charlist[0];
$matrix = explode($char,$str);
for($i=0; $i<sizeof($matrix); $i++){
    $matrix[$i] = expl($matrix[$i],substr($charlist,1));
}
return($matrix);
}
echo "<pre>";
print_r(expl("A~a1~a2|B~b1~b2",'|~'));
echo "</pre>";
?>

that would be something like this... use recursion! the firs level will get the first matrix, doing something like this $matrix[0] = "A~a1~a2"; $matrix[1] = "B~b1~b2"; and then, the recursion will do the second part, that will make each string become the array of strings, that will become the array of strings, until there's no more separators =)

这将是这样的...使用递归!第一级矩阵将得到第一个矩阵,做这样的$ matrix [0] =“A~a1~a2”; $ matrix [1] =“B~b1~b2”;然后,递归将执行第二部分,这将使每个字符串成为字符串数组,这将成为字符串数组,直到没有更多的分隔符=)

#2


1  

The below is a working example, and I've provided a link to show you the output when the function is run:

下面是一个工作示例,我提供了一个链接,显示函数运行时的输出:

Example return:

http://phpfiddle.org/api/run/4iz-i2x

Usage:

echo '<pre>';
print_r( expl("A~a1~a2|B~b1~b2",'|~'));
echo '</pre>';

Function:

<?php

function expl($str,$charlist='|', $currentChar = 0, $continue = true){

    if(!$continue)
    {
        return $str;
    }

    $endArray = array();

    if($currentChar == 0){
        $charlist = str_split($charlist);
    }
    else
    {
        if($currentChar > count($charlist))
        {
            return expl($str, $charlist, $currentChar, false);
        }
    }

    if(!is_array($str))
    {   
        $pieces = explode($charlist[$currentChar], $str);
        $currentChar++;
        return expl($pieces, $charlist, $currentChar);
    }
    else{
        foreach($str as $arrayItem){
        if(is_array($arrayItem))
        {
                return expl($str, $charlist, $currentChar, false);
        }

        $endArray[] = explode($charlist[$currentChar], $arrayItem);

        }


        $currentChar++;
        return expl($endArray, $charlist, $currentChar);                
    }               
}    

?>

#3


0  

Simply do like this.First explode by '|' then by '~'. it should be something like this:

只需这样做。首先爆炸'|'那么'〜'。它应该是这样的:

$str="A~a1~a2|B~b1~b2";
$arr=explode("|",$str);
$result=array();
foreach($arr as $k=>$v)
{
  $arr1=explode("~",$v);
  $result[]=$arr1;
}

#1


1  

<?php
function expl($str,$charlist='|'){
if(!$charlist) return($str);
$char = $charlist[0];
$matrix = explode($char,$str);
for($i=0; $i<sizeof($matrix); $i++){
    $matrix[$i] = expl($matrix[$i],substr($charlist,1));
}
return($matrix);
}
echo "<pre>";
print_r(expl("A~a1~a2|B~b1~b2",'|~'));
echo "</pre>";
?>

that would be something like this... use recursion! the firs level will get the first matrix, doing something like this $matrix[0] = "A~a1~a2"; $matrix[1] = "B~b1~b2"; and then, the recursion will do the second part, that will make each string become the array of strings, that will become the array of strings, until there's no more separators =)

这将是这样的...使用递归!第一级矩阵将得到第一个矩阵,做这样的$ matrix [0] =“A~a1~a2”; $ matrix [1] =“B~b1~b2”;然后,递归将执行第二部分,这将使每个字符串成为字符串数组,这将成为字符串数组,直到没有更多的分隔符=)

#2


1  

The below is a working example, and I've provided a link to show you the output when the function is run:

下面是一个工作示例,我提供了一个链接,显示函数运行时的输出:

Example return:

http://phpfiddle.org/api/run/4iz-i2x

Usage:

echo '<pre>';
print_r( expl("A~a1~a2|B~b1~b2",'|~'));
echo '</pre>';

Function:

<?php

function expl($str,$charlist='|', $currentChar = 0, $continue = true){

    if(!$continue)
    {
        return $str;
    }

    $endArray = array();

    if($currentChar == 0){
        $charlist = str_split($charlist);
    }
    else
    {
        if($currentChar > count($charlist))
        {
            return expl($str, $charlist, $currentChar, false);
        }
    }

    if(!is_array($str))
    {   
        $pieces = explode($charlist[$currentChar], $str);
        $currentChar++;
        return expl($pieces, $charlist, $currentChar);
    }
    else{
        foreach($str as $arrayItem){
        if(is_array($arrayItem))
        {
                return expl($str, $charlist, $currentChar, false);
        }

        $endArray[] = explode($charlist[$currentChar], $arrayItem);

        }


        $currentChar++;
        return expl($endArray, $charlist, $currentChar);                
    }               
}    

?>

#3


0  

Simply do like this.First explode by '|' then by '~'. it should be something like this:

只需这样做。首先爆炸'|'那么'〜'。它应该是这样的:

$str="A~a1~a2|B~b1~b2";
$arr=explode("|",$str);
$result=array();
foreach($arr as $k=>$v)
{
  $arr1=explode("~",$v);
  $result[]=$arr1;
}