将具有三个分隔符的字符串解析为索引数组的关联数组

发布于 2024-09-28 16:05:35 字数 864 浏览 7 评论 0原文

我有这行字符串

Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice Cream|apple pie

: (冒号)是分隔符主主题,| 是不同类型子主题的分隔符。

我尝试将其分解并放入数组中,我需要将结果显示在下拉菜单中:-

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

I have this line of string

Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice cream|apple pie

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
...

Code:

$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?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百善笑为先 2024-10-05 16:05:35

您可以执行以下操作:

$result = explode(":", $data);
foreach ($result as $res) {
    $sub = explode("-", $res);
    $mainCategory[$sub[0]] = explode("|", $sub[1]);
}

工作链接

You can do:

$result = explode(":", $data);
foreach ($result as $res) {
    $sub = explode("-", $res);
    $mainCategory[$sub[0]] = explode("|", $sub[1]);
}

Working link

远昼 2024-10-05 16:05:35

代码:

$data = "Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice cream|apple pie";
 foreach(explode(":",$data) as $res) { // explode by ":"
  $cat = explode("-",$res); // explode by "-"
  $ilovefood[$cat[0]] = explode("|",$cat[1]); // explode by "|"
 }
 print_r($ilovefood);
 //Returns : Array ( [Fruits] => Array ( [0] => banana [1] => apple [2] => orange ) [Food] => Array ( [0] => fries [1] => sausages ) [Desserts] => Array ( [0] => ice cream [1] => apple pie ) )
 foreach($ilovefood as $type=>$yum){
  echo "$type:<select>";
  foreach($yum as $tasty){
   echo "<option>$tasty</option>";
  }
  echo "</select>";
 }

更新以反映下拉列表的添加。看起来我刚刚做了你的作业,不过我将让你将所有内容合并到一个 foreach 循环中。

Code:

$data = "Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice cream|apple pie";
 foreach(explode(":",$data) as $res) { // explode by ":"
  $cat = explode("-",$res); // explode by "-"
  $ilovefood[$cat[0]] = explode("|",$cat[1]); // explode by "|"
 }
 print_r($ilovefood);
 //Returns : Array ( [Fruits] => Array ( [0] => banana [1] => apple [2] => orange ) [Food] => Array ( [0] => fries [1] => sausages ) [Desserts] => Array ( [0] => ice cream [1] => apple pie ) )
 foreach($ilovefood as $type=>$yum){
  echo "$type:<select>";
  foreach($yum as $tasty){
   echo "<option>$tasty</option>";
  }
  echo "</select>";
 }

Updated to reflect the drop-down addition. Looks like I just did your homework, though I'll leave it up to you to combine all the into one foreach loop.

一场信仰旅途 2024-10-05 16:05:35

我建议 Codeaddicts 的 答案

$str = "Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice cream|apple pie";

$topics = array();
foreach (explode(':', $str) as $topic) {
  list($name, $items) = explode('-', $topic);
  $topics[$name] = explode('|', $items);
}

I'd propose an arguably more readable version of codeaddicts' answer

$str = "Fruits-banana|apple|orange:Food-fries|sausages:Desserts-ice cream|apple pie";

$topics = array();
foreach (explode(':', $str) as $topic) {
  list($name, $items) = explode('-', $topic);
  $topics[$name] = explode('|', $items);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文