Results 1 to 3 of 3

Thread: Splitting Numbers

  1. #1
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Splitting Numbers

    I have this PHP code:
    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:
    Code:
    show_demo("1","2","5","6","9","10","11");
    what I need, which I am confused on, is this:
    Code:
    show_demo("1","2","5");
    show_demo("6","9","10");
    show_demo("11");
    I've been trying to figure this out, but nothing succeeded.

  2. #2
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb hi, try this

    Code:
    $a_gen = array(); $a_content = array(); $ic = count($gen['demo1']);
    for($i=0; $i < $ic; $i++) {
        if ($i > 0 && !($i % 3)) {
            $a_content[] = 'show_demo('. implode(',', $a_gen) .');';
            array_splice($a_gen, 0);
        }
        $a_gen[] = '"'. $gen['demo1'][$i] .'"';
    }
    if (count($a_gen) > 0) $a_content[] = 'show_demo('. implode(',', $a_gen) .');';
    $content = implode("\r\n", $a_content);
    unset($a_gen); unset($a_content); unset($ic);
    Last edited by Daniel+; 03-12-2007 at 10:42 PM.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function array_split($arr, $n) {
      $r = array();
      for($i = 0; $i < count($arr); $i += $n)
        array_push($r, array_slice($arr, $i, $n));
      return $r;
    }
    
    // ...
    
    foreach(array_split($gen['demo1'], 3) as $v)
      call_user_func_array('show_demo', $v);
    Last edited by Twey; 03-14-2007 at 12:10 AM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •