Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 47 guests online
 
Advertisement

You are here: Home arrow Ethical Hacking Discussions and Related Certificationsarrow Programmingarrow PHP Help
EH-Net
May 21, 2013, 07:17:09 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Go back to The Ethical Hacker Network Online Magazine Home Page
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP Help  (Read 6655 times)
0 Members and 1 Guest are viewing this topic.
BillV
Hero Member
*****
Offline Offline

Posts: 1892


View Profile WWW
« on: September 04, 2009, 10:05:48 PM »

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!
Logged
payup
Newbie
*
Offline Offline

Posts: 1


View Profile
« Reply #1 on: September 04, 2009, 11:09:20 PM »

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;
}
?>

Logged
Jhaddix
Sr. Member
****
Offline Offline

Posts: 317



View Profile WWW
« Reply #2 on: September 04, 2009, 11:16:28 PM »

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

BillV
Hero Member
*****
Offline Offline

Posts: 1892


View Profile WWW
« Reply #3 on: September 05, 2009, 07:51:44 AM »

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

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.
Logged
Ketchup
Hero Member
*****
Offline Offline

Posts: 1021



View Profile
« Reply #4 on: September 05, 2009, 12:15:03 PM »

Bill,

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

~~~~~~~~~~~~~~
Ketchup
BillV
Hero Member
*****
Offline Offline

Posts: 1892


View Profile WWW
« Reply #5 on: September 05, 2009, 03:30:21 PM »

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:

Quote
Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 2097152 bytes)
Logged
ethicalhack3r
Full Member
***
Offline Offline

Posts: 139


View Profile WWW
« Reply #6 on: September 05, 2009, 07:06:56 PM »

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:

Quote
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.
Logged
UNIX
Hero Member
*****
Offline Offline

Posts: 1235


View Profile
« Reply #7 on: September 06, 2009, 12:59:02 PM »

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.
Logged
BillV
Hero Member
*****
Offline Offline

Posts: 1892


View Profile WWW
« Reply #8 on: September 07, 2009, 07:51:03 AM »

Yeah, I'm aware of it.. I don't have the ability to change it in the hosting environment.
Logged
Ketchup
Hero Member
*****
Offline Offline

Posts: 1021



View Profile
« Reply #9 on: September 07, 2009, 09:47:43 AM »

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

~~~~~~~~~~~~~~
Ketchup
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.064 seconds with 22 queries.
 
Exclusive Deal

sansfire13_245x90_cw90.jpg
SANSFIRE 2013
June 15 - 22

5% Off w/ Code: EHN_5

SANS Deals 4 EH-Netters
5% OFF Any SANS Course in Any Format!
Coupon Code: EHN_5 Including SANS Rocky Mountain 2013 & SANS Boston 2013
Polls
Compared to this year, 2013 will be:
 
Recent Forum Topics
EH-Net News Feeds
Latest Additions
 
         
Advertisement

© 2013 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.