1

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

3 Answers 3

2
<?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 first 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, which will make each string become the array of strings, that will become the array of strings until there's no more separators.

Sign up to request clarification or add additional context in comments.

3 Comments

by the way... i havent tested it! it's just an idea of an algorithm!
Thanks Magnus - this is exactly what I was trying to do with my function. I've edited your answer to clean up the syntax in your example but I still dont think it's correct. Have I made a mistake somewhere? It's outputting something like Array(0=>Array(0=>'~'))
Great - I found the problem and updated your answer. Hopefully my edit will be peer-accepted
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);                
    }               
}    

?>

2 Comments

Thanks Evan - It definitely works. It feels a little convoluted but since I dont have any ideas on how to simplify it, I suppose it will do nicely for now.
@cronoklee Alright, and yes I agree with you but I was rushing to build this. Perhaps you can improve on it :)
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 Comment

Thanks Gerry but the idea is to automate it so you can have dynamic delimiters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.