The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 21, 2019, 12:11:17 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: how do you shuffle a 2 dimensional array?  (Read 3312 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« on: February 05, 2009, 01:35:03 PM »

shuffle() apparently aint gonna do it.

so sample array is
expensive car=>http://www.domain.com
expensive cartoon=>http://www.domain2.com
cheap car porn=>http://www.domain3.com
« Last Edit: February 12, 2009, 09:25:37 PM by perkiset » Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: February 05, 2009, 01:47:53 PM »

confused - where's the second d? that looks like a straight up associative to me.

off the top, if you brought everything back from your DB as

$db->query("select theKey, theDomain from mytable");
$ptr = 0;
while ($db->fetchRow())
{
   $arr[$ptr]['key'] = $db->row[0];
   $arr[$ptr]['domain'] = $db->row[1];
   $ptr++;
}
shuffle($arr);

I think you'd do it, because it'd shuffle the contents of the array, which is arrays, but the 2d array would not be touched. Then if you MUST have it as a straight associative, you'd simply

foreach($arr as $record) $newArr[$record['key']] = $record['domain'];

and you'd have it.

Methinks.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #2 on: February 05, 2009, 01:55:09 PM »

I can't remember the last time I used a multi-dimensional array. They just make my brain hurt. It's probably much slower and less efficient, but I just make arrays of objects with all the data I need.
Logged

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: February 05, 2009, 02:13:14 PM »

sorry im meant associative array.  D'oh!

sheesh. to many irons, not enough neurons.

anywho...

it seems tremendously weird to me that there is a shuffle function, the ONLY works with numeric keys... wtf?

unfortunately this is not from a database, otherwise, I would actually do it on the DB side with rand. This is actually a serialized array that needs to be randomly shuffled each time it is used.

Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: February 05, 2009, 02:55:43 PM »

Well, if you think about it, shuffling an associative array really doesn't make all that much sense, except when you foreach. You'll need to move the array from the original into another, so that the foreach works sequentially as the array was put together. Here is another concept that might do you:

$inArr = unserialize($arrStr);

foreach($inArr as $key=>$value)
   $keys[] = $key;

shuffle($keys)

foreach($keys as $key)
   $outArr[$key] = $inArr[$key];

Kind of funky looking, but I'll bet that'd do you pretty durn quickly.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #5 on: February 09, 2009, 11:32:06 AM »

Did you ever try this, and did it work for you Nuts?
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
netmktg
Rookie
**
Offline Offline

Posts: 37



View Profile
« Reply #6 on: February 18, 2009, 03:21:48 AM »

shuffle() apparently aint gonna do it.

so sample array is
expensive car=>http://www.domain.com
expensive cartoon=>http://www.domain2.com
cheap car porn=>http://www.domain3.com

So, are you still looking for a solution  Nerd

The answer is in shuffle() itself but its how you arrange the data before shuffling. It requires a one line of code before shuffle in which you assign the existing data into a new Array.

Code:
foreach ($input_arr as $k=>$v) { $output_arr[] = array($k=>$v); }
shuffle($output_arr);

The elements returned from Output_Array are associate Array elements, so you just them as Key=>Values
« Last Edit: February 18, 2009, 03:23:33 AM by netmktg » Logged
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #7 on: February 19, 2009, 11:03:43 AM »

i will try both your and perks.
your seems simpler, but I have to see how it works first.

i just dont have time right now to deal with this, thats why its been unanswered by me.

you know in movies when the pipe is under so much pressure that they start putting clamps and shit around it as it starts developing tears and cracks. yea, thats me right now...

lol?
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: February 19, 2009, 11:29:26 AM »

I was thinking more like submariners that suddenly have the nasty high-pressure hull breach and there is mad desperation to put ANYTHING to hold the structure and stop the leakage.

Not LOL because it hits too close to home  Undecided
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #9 on: March 21, 2009, 07:51:59 AM »

I am confused ?
You want to shuffle it as in when you go over it with foreach it will be in a random order ?
just get all the key values put into a simple array, then shuffle that, then walk over the shuffled keys looking up each value.
or put the keys , value pairs into tuples, shuffle that and walk over that, probably that would be quicker
Logged
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: March 21, 2009, 10:43:43 AM »

That was my post exactly nop, 5 up. The weirdness is that an indexed array is by definition ordered, so a shuffle makes sense - an associative is by definition not ordered by indexed for quick lookup, so a shuffle makes no sense ... EXCEPT if you are doing a foreach($arr as $name=>$value). So putting the keys into an ordered array and shuffling then rebuilding the associative is the only real way to do it.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!