I have this PHP code:

<?php
$gen["demo1"][] = 1;
$gen["demo1"][] = 2;
$gen["demo1"][] = 5;
$gen["demo1"][] = 6;
$gen["demo1"][] = 9;
$gen["demo1"][] = 10;
$gen["demo1"][] = 11;
//etc...
$content = "show_demo(";
for($i=0; $i < count($gen["demo1"]); $i++) {
$content .= "\"".$gen["demo1"][$i]."\",";
}
$content .= ")";
$content = str_replace("\",)","\")", $content);
echo $content;
?>

which basically outputs:

show_demo("1","2","5","6","9","10","11");

what I need, which I am confused on, is this:

show_demo("1","2","5");
show_demo("6","9","10");
show_demo("11");

I've been trying to figure this out, but nothing succeeded. 😕

    Could you please explain the logic of what the code should do?

    In any case.. I do not understand what it should do, so I can only create a code that reproduces your output not something that will always waork..

    $gen["demo1"] = array(1, 2, 5, 6, 9, 10, 11);
    $i=0;
    $content = "";
    foreach($gen["demo1"] as $value)
      {
      if($i%3 == 0)
        {
        if($content <> "")
          {
           $content .=  "\")<br />";
          }
        $content .= "show_demo(";
        }
      $content .= "\"".$value."\",";
      $i++;
      }
    
    if(($i-1)%3 <> 0)
      {
      $content .=  "\")<br />";
      }
    
    echo $content; 
    
    
    

      That is what I'm looking for, although it outputs:

      show_demo("1","2","5",")
      show_demo("6","9","10",")
      show_demo("11",

      instead of

      show_demo("1","2","5")
      show_demo("6","9","10")
      show_demo("11")

      I can try and tweak it but thanks!

      edit: The explanation is this is for a PHP eval() to output several tables of JavaScript demos, although nobody wants every row stretched out so far they have to scroll left and right to see it!

        Try replacing this:

        $content = str_replace("\",)","\")", $content); 

        with this:

        $content = str_replace('",")','")', $content); 

          Or use the programming principle that smart data and dumb code is better than dumb data and smart code, and put the data into a structure you want before trying to get fancy with manipulating it. (It's easier when the data says what it is instead of having to tell it all the time.)

          $demos = $gen['demo'];
          
          // Put everything in quotes.
          foreach($demos as &$demo)
          {
              $demo = '"'.$demo.'"';
          }
          unset($demo);
          
          // Split the list into groups of three
          $demos = array_chunk($demos, 3);
          
          // Build the content string.
          $content = "";
          foreach($demos as $demo)
          {
          $content .= "show_demos(".join(", ", $demo).");\n";
          }
          
            Write a Reply...