Php agrupate阵列中的元素爆炸

时间:2017-12-11 15:59:53

标签: php arrays explode

我需要分解不同的值,同时使用不同的分隔符,在本例中为“*”和“,”,并且我希望组元素具有相同的元素,在本例中为“car1 “

使用示例,我在使用爆炸时尝试使用相同元素的组元素,但最后不要

        <?php
/// Values to Explode
        $a="house1,car1,phone1*house2,car1,phone2*house3,car3,phone3*";
/// First Explode
        $exp_1=explode("*",$a);

/// Loop Explode with "*"
        foreach($exp_1 as $exps)
        {
        $exps=explode(",",$exps);

    /// Here i want group the arrays or elements contain the same - car1 -                in this case 1 and 2 arrays

    /// I try do this but don´t works ///

        if ($exps[1]==$exps[1])
        {
        /// Must show house1,car1,phone1 and house2,car1,phone2 because have the same car called car1, and group en each case, common element it´s car1
        print "ok";
        }
        else
        {
        print "Others";
        }

        }

        ?>

1 个答案:

答案 0 :(得分:0)

使用preg split:

$a = "house1,car1,phone1*house2,car1,phone2*house3,car3,phone3*";
$parts = preg_split('/[*|,]/', $a);
var_dump ($parts);