Be Excellent To Each Other

And, you know, party on. Dude.

All times are UTC [ DST ]




Reply to topic  [ 25 posts ] 
Author Message
 Post subject: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:05 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
I can't seem to get my head around the logic of this.

Say you have a multi-array:
Code:
Array
(
    [0] => Array
        (
            [0] => $value
            [1] => $value
        )

    [1] => Array
        (
            [0] => $value
            [1] => $value
            [2] => $value
            [3] => $value
        )

    [2] => Array
        (
            [0] => $value
            [1] => $value
        )
)

That array can have any number of values in the first array, and any number in the second, but it will only ever be two deep.

So, using the one above as an example, I want to step through it 'sideways' to get every possible combination of values, so
Code:
array[0][0]
array[1][0]
array[2][0]

array[0][0]
array[1][0]
array[2][1]

array[0][0]
array[1][1]
array[2][0]

array[0][0]
array[1][1]
array[2][1]

array[0][0]
array[1][2]
array[2][0]

array[0][0]
array[1][2]
array[2][1]

array[0][0]
array[1][3]
array[2][0]

array[0][0]
array[1][3]
array[2][1]

array[0][1]
array[1][0]
array[2][0]

array[0][1]
array[1][0]
array[2][1]

array[0][1]
array[1][1]
array[2][0]

array[0][1]
array[1][2]
array[2][0]
...

Etc.
There must be a way of doing it with loops and I've been trying for two hours and I can't and it's driving me fucking mad.
FIX!

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:13 
User avatar
Be Excellent Member

Joined: 30th Mar, 2008
Posts: 98
We're talking PHP presumably?

You'll have to iterate through them yourself, either by:
1) taking array_keys() and building a list (in another array) of where you're up to.
or
2) dicking about with the internal array pointers with functions like 'reset', 'current' and 'next'

The latter's probably faster, but the former might be more sane to code.

EDIT: Wait, perhaps I misunderstand...


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:21 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
There might be an easier way than that - I think I'm getting too caught up in the order they come back in when it doesn't really matter, just as long as they all come back.
So three embedded loops should do it.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:24 
Excellent Member

Joined: 15th May, 2009
Posts: 368
That doesn't quite make sense to me I think they are 1-dimensional arrays though, and you have an array of arrays. Take a mental step back. Your outer loop will be the number of arrays you want to loop through. The next loop will be the number of records in each array. What are you working in?

Edit I see someone has replied. I shall post this anyway


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:25 
User avatar
That Rev Chap

Joined: 31st Mar, 2008
Posts: 4924
Location: Kent
foreach?


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:31 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
IFeelAsleep wrote:
Take a mental step back. Your outer loop will be the number of arrays you want to loop through. The next loop will be the number of records in each array. What are you working in?

Whatever you can demonstrate in is fine - it's a logic problem rather than a coding one.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:36 
User avatar
Excellent Member

Joined: 25th Nov, 2008
Posts: 1041
for ( int i = 0; i < outerArray.Length; i++ )
{

for ( int j = 0; j < outerArray[i].Length; j++ )
{

doSomethingWith(outerArray[i][j]);

}

}

Wouldn't that do it?

_________________
Image


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 19:38 
Excellent Member

Joined: 15th May, 2009
Posts: 368
Right oh. Give me a mo, I am just firing up visual studio. So that will be about half an hour, plus the time it takes me to work something out.

Edit: err, yeah, what End of an Era said.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 20:00 
User avatar
Be Excellent Member

Joined: 30th Mar, 2008
Posts: 98
No attempt made to make it pretty... this should spit out in the original order you asked for. What do I win?

Code:
<?php

$parseJunk = Array
(
    '0' => Array
        (
            '0' => '0-0',
            '1' => '0-1',
        ),

    '1' => Array
        (
            '0' => '1-0',
            '1' => '1-1',
            '2' => '1-2',
            '3' => '1-3',
        ),

    '2' => Array
        (
            '0' => '2-0',
            '1' => '2-1',
        ),
);


// Build first 'currentKey' and 'topLevelKeyCount' as the limiter
$topLevelKeyCount = array();
$currentKey = array();
foreach( array_keys( $parseJunk ) as $topLevelKey ) {
   $topLevelKeyCount[] = count( $parseJunk[ $topLevelKey ] );
   $currentKey[] = 0;
}
$subKeyCount = count( $topLevelKeyCount );



do {
   echo 'Grouping: ';
   for( $subKeyIndex = 0; $subKeyIndex < $subKeyCount; ++$subKeyIndex ) {
      echo $parseJunk[ $subKeyIndex ][ $currentKey[ $subKeyIndex ] ].' ';
   }
   echo '<br />';
} while( $currentKey = nextKeyGet( $topLevelKeyCount, $currentKey ) );




// returns false if finished, or next 'key'
function nextKeyGet( $pKeyCount, $pCurrentKey ) {
   $subkeyIndex = count( $pCurrentKey ) - 1;

   while( $subkeyIndex >= 0 ) {
      if( $pCurrentKey[ $subkeyIndex ] + 1 < $pKeyCount[ $subkeyIndex ] ) {
         $pCurrentKey[ $subkeyIndex ] ++;
         return $pCurrentKey;
      }

      $pCurrentKey[ $subkeyIndex ] = 0;
      $subkeyIndex --;
   }

   return false;
}


?>


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 20:02 
Excellent Member

Joined: 15th May, 2009
Posts: 368
Show off! ;)


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Tue Aug 04, 2009 20:03 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
Jesus, I thought it'd be simpler than that. Thanks man, I'll take a look tomorrow. I not going to think about this any more tonight.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 9:40 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
SkyKid wrote:
What do I win?

Not much, it just outputs the same thing 16 times ;)

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 9:41 
User avatar
Gogmagog

Joined: 30th Mar, 2008
Posts: 48897
Location: Cheshire
Grim... wrote:
SkyKid wrote:
What do I win?

Not much, it just outputs the same thing 16 times ;)


Like myp when he posts links, then.

_________________
Mr Chris wrote:
MaliA isn't just the best thing on the internet - he's the best thing ever.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:16 
User avatar
MR EXCELLENT FACE

Joined: 30th Mar, 2008
Posts: 2568
I don't know PHP, nor do I care to, but in other languages it's just:

Code:
    for length of array
        for length of sub-array


(edit: a quick google says it's "count()")

edit: I don't do PHP, but adapting the above answer and guessing:
Code:
for (i = 0; i < conut(array); i++ )
{
    for ( int j = 0; j < count(array[i]); j++ )
    {
        doSomethingWith(array[i][j]);
    }
}



also: Why not try asking these sorts fo things on Stackoverflow ?

_________________
This man is bound by law to clear the snow away


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:19 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
No it's not.
I need every combination of x arrays.

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:22 
User avatar
MR EXCELLENT FACE

Joined: 30th Mar, 2008
Posts: 2568
That is?

Array[2][2]:

0,0
0,1
1,0
1,1

isn't that every combination?

_________________
This man is bound by law to clear the snow away


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:27 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
I have simplified the first post :)

Is anyone a member of expert sexchange?

http://www.experts-exchange.com/Web_Dev ... 80571.html

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:30 
User avatar
MR EXCELLENT FACE

Joined: 30th Mar, 2008
Posts: 2568
experts exchange is shit. Use stackoverflow instead.

(also: it would help if your array exampel didn't go up to [1][4] [1][3] ;))

TBH: I don't understand what you're trying to do. If you take your orignial output and do this:

H:\>cat pooooo.txt | sort | uniq
array[0][0]
array[0][1]
array[1][0]
array[1][1]
array[1][2]
array[1][3]
array[1][4]
array[2][0]
array[2][1]

notice how that's exactly the same as a normal loopover? What exactly are you trying to do?

_________________
This man is bound by law to clear the snow away


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:32 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
This:
Code:
        function findcombination($a){
           $out = array();
           if (count($a) == 1) {
              $x = array_shift($a);
              foreach ($x as $v) $out[] = array($v);
              return $out;
           }
           foreach ($a as $k=>$v){
              $b = $a;
              unset($b[$k]);
              $x = findcombination($b);
              foreach ($v as $v1){
                 foreach ($x As $v2)
                 $out[] = array_merge(array($v1), $v2);
              }
           }
           return $out;
        }


Thanks all!

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:33 
SupaMod
User avatar
Est. 1978

Joined: 27th Mar, 2008
Posts: 69713
Location: Your Mum
Pod wrote:
(also: it would help if your array exampel didn't go up to [1][4] [1][3] ;))

Oh yeah, oops :)

_________________
Grim... wrote:
I wish Craster had left some girls for the rest of us.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 10:37 
User avatar
Be Excellent Member

Joined: 30th Mar, 2008
Posts: 98
I did test the original code - it was working for me 8)

Here's an alternative, if you don't care about order. Looks like you've got a recursive one already tho...
Code:
parseLevel( $parseJunk, 0, array() );

function parseLevel( $pJunkArray, $pLevel, $pKeyList ) {
// terminate...
   if( $pLevel == count( $pJunkArray ) ) {
      echo 'group: ';
      for( $iKeyIndex = 0; $iKeyIndex < $pLevel; $iKeyIndex ++ ) {
         echo "[{$iKeyIndex}-{$pKeyList[$iKeyIndex]} = {$pJunkArray[ $iKeyIndex ][ $pKeyList[$iKeyIndex] ]}] ";
      }
      echo '<br />';
      return;
   }

// recurse...
   $nextLevel = $pLevel + 1;
   foreach( array_keys( $pJunkArray[ $pLevel ] ) as $pKey ) {
      $newKeyList = $pKeyList;
      $newKeyList[] = $pKey;
      parseLevel( $pJunkArray, $nextLevel, $newKeyList );
   }
}


Now... back to my own work :(


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 11:06 
User avatar

Joined: 31st Mar, 2008
Posts: 8655
Grim... wrote:
I have simplified the first post :)

Is anyone a member of expert sexchange?

http://www.experts-exchange.com/Web_Dev ... 80571.html


Scroll all the way to the bottom of the page to see the answers on experts exchange (although this doesn't work on the mobile site, which can be a bit annoying).


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 12:01 
SupaMod
User avatar
Commander-in-Cheese

Joined: 30th Mar, 2008
Posts: 49244
I thought that, then I clicked on Grim...'s link, and they weren't there.

And now they're there again. Odd.

_________________
GoddessJasmine wrote:
Drunk, pulled Craster's pork, waiting for brdyime story,reading nuts. Xz


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 12:02 
User avatar

Joined: 30th Mar, 2008
Posts: 32624
That only seems to work sometimes on that crappy site. The google cache is the best way, for example:

http://209.85.229.132/search?q=cache:nP ... =firefox-a

Edit -- it's something to do with the SEO, of course.


Top
 Profile  
 
 Post subject: Re: Array issues kill death
PostPosted: Wed Aug 05, 2009 12:42 
User avatar

Joined: 31st Mar, 2008
Posts: 8655
Oh sorry, I think they've always been there when I've looked and I must admit I didn't actually click on Grim...'s link.


Top
 Profile  
 
Display posts from previous:  Sort by  
Reply to topic  [ 25 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search within this thread:
You are using the 'Ted' forum. Bill doesn't really exist any more. Bogus!
Want to help out with the hosting / advertising costs? That's very nice of you.
Are you on a mobile phone? Try http://beex.co.uk/m/
RIP, Owen. RIP, MrC. RIP, Dimmers.

Powered by a very Grim... version of phpBB © 2000, 2002, 2005, 2007 phpBB Group.