Php agrupate elements in array with explode

巧了我就是萌 提交于 2020-01-17 14:35:12

问题


I need explode different values, and at the same time, with differents delimiters, in this case "*" and ",", and i want group elements with the same element in common, in this case "car1"

With the example i try group elements with the same element when use explode, but don´t get finally

        <?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:


Use preg split:

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


来源:https://stackoverflow.com/questions/47756708/php-agrupate-elements-in-array-with-explode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!