I need a few pointers (...no, bad pun. I'm sorry.)

    Date: 07/23/06 (PHP Community)    Keywords: no keywords

    I'm almost certain that the answer to my problem can be found somewhere in the documentation, however, I can't seem to find it. In fact, the documentation is just confusing me. So I'm asking here. Can someone help?


    I've created a loop which, in theory, should cycle through each element in an array, extract a specific number of elements from a second array, then insert those elements as a new array into the current element of the first array. In other words, I'm going from this:

    $first_array[] = array();
    $first_array[] = array();
    $first_array[] = array();

    $second_array[] = "string1";
    $second_array[] = "string2";
    $second_array[] = "string3";
    $second_array[] = "string4";
    $second_array[] = "string5";
    $second_array[] = "string6";
    $second_array[] = "string7";

    ...to this:

    $first_array[] = array(string1, string2, string3);
    $first_array[] = array(string4, string5);
    $first_array[] = array(string6, string7);


    Here's the problem. I can extract the appropriate number of elements from the second array easily enough, but I can't extract the specific ones I want. The second array's internal pointer seems to be resetting itself each time the loop moves to a new element in the first array, so I end up with this:

    $first_array[] = array(string1, string2, string3);
    $first_array[] = array(string1, string2);
    $first_array[] = array(string1, string2);


    What I can't figure out is why. I'm not sure if the internal pointer is actually resetting itself on each iteration, or if it never moves in the first place. I somewhat suspect the latter, as nothing I've tried in an attempt to push it back to the appropriate key seems to work. But if it's not moving, how on earth is it fetching any elements at all? Argh, does anyone know what's going on here? Here's the actual code, if it makes things clearer:


    Note: "chapters" refers to the first array; "pages" to the second.

       $i = 0;
       $number_of_chapters = count($chapters);
    
       foreach ($pages as $key => $value)
       {
         for ($current_chapter = $i; $current_chapter < $number_of_chapters; $current_chapter++)
         {
           if ($key < $number_of_pages_in_current_chapter)
           {
             $chapters[$current_chapter][] = $value;
           }
         }
       }

    I know the documentation warns that foreach automatically resets the pointer, however using each() and list() instead made no difference. Furthermore, I can't quite see how or why it should reset after each iteration of the for loop, since that's nestled within the foreach. Foreach shouldn't reset until its *own* iteration is complete, should it?


    ...I just bet the answer to this is going to be something ridiculously simple like "you've got an extra semicolon". DAMN THOSE SEMICOLONS.

    Source: http://community.livejournal.com/php/474003.html

« Input variable checking || For everyone who was... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home