如何将逗号分隔的字符串与管道转换为两个不同的数组?
$combo_string = 'blue|car, red|truck, green|boat, yellow|bike';
我需要的地方:
$colors_array =
Array (
[0] => blue
[1] => red
[2] => green
[3] => yellow
)
和
$vehicles_array =
Array (
[0] => car
[1] => truck
[2] => boat
[3] => bike
)
$combo_array = explode(', ', $combo_string);
$colors_array = $vehicles_array = array();
foreach ($combo_array as $piece) {
list($color, $vehicle) = explode('|', $piece);
$colors_array[] = $color;
$vehicles_array[] = $vehicle;
}
我还没有测试下面的代码,但它应该是大致正确的:
$combo_string = 'blue|car, red|truck, green|boat, yellow|bike';
$initial_array = explode(", " $combo_string);
$vehicles_array = array();
$colors_array = array();
foreach ($combo_string as $item)
{
$seperate_colour = explode("|", $item);
$colors_array[] = $seperate_colour[0];
$vehicles_array[] = $seperate_colour[1];
}