问题
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