.

PHP Help

<<

venom77

User avatar

Hero Member
Hero Member

Posts: 1905

Joined: Mon Dec 11, 2006 3:23 pm

Post Fri Sep 04, 2009 10:05 pm

PHP Help

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...

  Code:
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!
<<

payup

Newbie
Newbie

Posts: 1

Joined: Sun Feb 15, 2009 8:23 pm

Post Fri Sep 04, 2009 11:09 pm

Re: PHP Help

  Code:
<?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['all'][] = $arrayitem;
   }
   $arraynum++;
      if(${'myArrayName'.$arraynum}!=""){
         merge($arraynum);
      }   
      else{
         return $GLOBALS['all'];
      }
}

merge(1);

foreach ($all as $arrayitem){
   echo $arrayitem;
}
?>
<<

Jhaddix

User avatar

Sr. Member
Sr. Member

Posts: 317

Joined: Wed Oct 29, 2008 10:25 pm

Post Fri Sep 04, 2009 11:16 pm

Re: PHP Help

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

venom77

User avatar

Hero Member
Hero Member

Posts: 1905

Joined: Mon Dec 11, 2006 3:23 pm

Post Sat Sep 05, 2009 7:51 am

Re: PHP Help

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.
<<

Ketchup

User avatar

Hero Member
Hero Member

Posts: 1021

Joined: Fri Jul 04, 2008 7:44 pm

Location: Philadelphia, PA

Post Sat Sep 05, 2009 12:15 pm

Re: PHP Help

Bill,

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

venom77

User avatar

Hero Member
Hero Member

Posts: 1905

Joined: Mon Dec 11, 2006 3:23 pm

Post Sat Sep 05, 2009 3:30 pm

Re: PHP Help

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)
<<

ethicalhack3r

Full Member
Full Member

Posts: 139

Joined: Fri Nov 28, 2008 11:29 am

Post Sat Sep 05, 2009 7:06 pm

Re: PHP Help

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.
<<

UNIX

User avatar

Hero Member
Hero Member

Posts: 1244

Joined: Mon Apr 28, 2008 9:20 am

Post Sun Sep 06, 2009 12:59 pm

Re: PHP Help

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.
<<

venom77

User avatar

Hero Member
Hero Member

Posts: 1905

Joined: Mon Dec 11, 2006 3:23 pm

Post Mon Sep 07, 2009 7:51 am

Re: PHP Help

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

Ketchup

User avatar

Hero Member
Hero Member

Posts: 1021

Joined: Fri Jul 04, 2008 7:44 pm

Location: Philadelphia, PA

Post Mon Sep 07, 2009 9:47 am

Re: PHP Help

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.
~~~~~~~~~~~~~~
Ketchup

Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software