Parse a string with three delimiters into an associative array of indexed arrays
Asked Answered
S

3

5

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?
}
Soapberry answered 25/10, 2010 at 19:0 Comment(4)
Do you need separate 2-dimensional array of sub-categories?Apollonian
Are you needing one drop-down for all or a separate drop-down for each type?Untrue
Doesn't matter if it's 2 dimensional array or not :)Soapberry
The line is auto generated from a system. I need only one drop down.Soapberry
M
6

You can do:

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

Working link

Micco answered 25/10, 2010 at 19:22 Comment(0)
U
3

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.

Untrue answered 25/10, 2010 at 19:12 Comment(2)
Thanks Jason! I do not need 3 drop down but your answer did help me to understand better:DSoapberry
I just realized that I have $ilovefood[$cat[0]] I don't actually love to each cats.Untrue
I
1

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);
}
Illiterate answered 25/10, 2010 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.