PHP Help

Viewing 9 reply threads
  • Author
    Posts
    • #4238
      BillV
      Participant

      Hey guys, I have a small piece of code I’m trying to make dynamic and I’m a little stumped – it’s been driving me nuts for a little while now.. was wondering if anyone might have some insight…


      foreach($w1 as $v1){
          foreach($w2 as $v2){
              foreach($w3 as $v3){
                  $all_w[]=$v1.$v2.$v3;
              }
          }
      }

      I’m trying to figure out how I can make that dynamic, so I don’t have to have 50 nested foreach loops… I think I can get the first part, but building the array at the end seems to be the tough part. Any suggestions?

      Thanks!

    • #26846
      payup
      Participant

      <?php
      $all = array();
      function merge($arraynum){
      ////// whatever you are merging, typically this won't be here either, but would be defined elsewhere and passed to the function
      static $myArrayName1 = array ('a','b');
      static $myArrayName2 = array ('c','d','e');
      static $myArrayName3 = array ('f','g','h','i');
      //////
      foreach (${'myArrayName'.$arraynum} as $arrayitem){
      $GLOBALS[] = $arrayitem;
      }
      $arraynum++;
      if(${'myArrayName'.$arraynum}!=""){
      merge($arraynum);
      }
      else{
      return $GLOBALS;
      }
      }

      merge(1);

      foreach ($all as $arrayitem){
      echo $arrayitem;
      }
      ?>
    • #26847
      Jhaddix
      Participant

      Can you describe in words what you were trying to accomplish?

    • #26848
      BillV
      Participant

      Thanks payup! That looks like it should do the trick.. will have to try it out 🙂

      Jason:
      I have an unknown amount of arrays ($w) that contain an unknown amount of elements ($v) each. The basics of it, is that I needed to find every possible combination always starting with an item from the first, then the second, etc.

      So if…

      $w = a, b, c
      $w2 = d, e, f

      I need to find:

      ad, ae, af, bd, be, bf, cd, ce, cf

      And this could of course be something more intense:

      $w = a, b, c, d
      $w2 = h, j, l, 1, 4, 6, $
      $w3 = 5, 2
      etc.

      The code I was using above worked really well and did the trick, but only up to the amount of ‘foreach’ that I had.

    • #26849
      Ketchup
      Participant

      Bill,

      Can you use a two dimensional array here, instead of having a bunch of one dimensional arrays?

    • #26850
      BillV
      Participant

      I probably could, but I think I’ve come to the realization that it doesn’t matter at this point. I have to cap it at 5 or 6 deep. Anything past that causes PHP to error out complaining about memory:

      Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 2097152 bytes)

    • #26851
      ethicalhack3r
      Participant

      @BillV wrote:

      I probably could, but I think I’ve come to the realization that it doesn’t matter at this point. I have to cap it at 5 or 6 deep. Anything past that causes PHP to error out complaining about memory:

      Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 2097152 bytes)

      I think you can increase the alocated memory size in PHP.ini. Probably best to find a better solution tho.

    • #26852
      UNIX
      Participant

      If you can’t find another solution the entry for changing the allocated memory would be at memory_limit if I remember correctly and if you haven’t found it yet.

    • #26853
      BillV
      Participant

      Yeah, I’m aware of it.. I don’t have the ability to change it in the hosting environment.

    • #26854
      Ketchup
      Participant

      I find that it’s usually better to optimize your code than to increase memory capacity. 

      Bill, can you parse the arrays in steps?  If you don’t find what you are looking for in 5 arrays, mark them as such and start another loop for the next 5, and so on? 

      Would they let you stick a small CGI in the hosting environment.  Maybe you can write this procedure in C, Perl, etc. and offload it from PHP.  Obviously this could completely run away with RAM.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.

Copyright ©2020 Caendra, Inc.

Contact Us

Thoughts, suggestions, issues? Send us an email, and we'll get back to you.

Sending

Sign in with Caendra

Forgot password?Sign up

Forgot your details?