PHP爆炸并投入数组 [英] PHP explode and put into array
本文介绍了PHP爆炸并投入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的线串
水果 - 香蕉|苹果|橙:食品薯条|香肠:甜品,冰淇淋|苹果派
在:
(冒号)是主要议题分隔符,并在 |
为分隔符不同类型的子课题。
the :
(colon) is the separator for the main topic, and the |
is the separator for the different type of sub topics.
我试图引爆出来,放入数组,我需要的结果是这样的,以显示在下拉菜单中: -
I tried to explode it out and put it into array, I need the result to be something like this to be displayed in a drop down menu:-
Fruits
banana
apple
orange
Food
fries
sausages
$result=explode(":",$data);
foreach($result as $res) {
$sub_res[]=explode("-",$res);
}
foreach($sub_res as $sub) {
//echo $sub[1]."<br>"; Over here, I can get the strings of [0]=>banana|apple|orange, [1]=>sausages|fries,
// I explode it again to get each items
$items[]=explode("|",$sub[1]);
$mainCategory[]=$sub[0]; // This is ([0]=>Fruits, ]1]=>Food, [2]=>dessert
// How do I assign the $items into respective categories?
}
谢谢!
推荐答案
您可以这样做:
$result=explode(":",$data);
foreach($result as $res) {
$sub = explode("-",$res);
$mainCategory[$sub[0]] = explode("|",$sub[1]);
}
这篇关于PHP爆炸并投入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文