Jump to content

Reading an Array


Shimmeh

Recommended Posts

Hello all,

 

I realize this is my first post but I've been visiting PHP Freaks for years.  I'm trying to accomplish something that I just can't seem to wrap my head around programmatically.  Say I've got a multidimensional array with each subarray containing 4 numbers (1,1,1,1 - 2,2,2,2 - 3,3,3,3 - 4,4,4,4).  How would I go about reading this multidimensional array in the sense that the output gives me 1,1,1,2 - 1,1,2,2, - 1,2,2,2, - 2,2,2,2 - 1,1,1,3 ..... 1,1,2,3 - 1,2,2,3 - 2,2,2,3 .... 3,3,3,4 - 3,3,4,4, - 3,4,4,4 - 4,4,4,4?

 

I hope I explained this well enough, if not please feel free to ask any questions and I'll be as pompt and helpful as I can at trying to explain what I'm trying to accomplish.

 

Thank you!

Link to comment
Share on other sites

Your array

$sets = array(  array(1,1,1,1),
                array(2,2,2,2),
                array(3,3,3,3),
                array(4,4,4,4)  );

I think this is a classic case of "if you want to go there I wouldn't start from here".

 

Rearrange the array so you have

$sets = array(  array(1,2,3,4),
                array(1,2,3,4),
                array(1,2,3,4),
                array(1,2,3,4)  );

This code will do that for you:

function rearrange($sets) {
    $new = array();
    foreach ($sets as $r => $row) {
        foreach ($row as $c => $val) {
            $new[$c][$r] = $val;
        }
    }
    return $new;
}

$sets = rearrange($sets);

Then you can use a recursive function to take each array in turn and combines with the others

function combys($sets, &$results, $x) {
    $ks = count($sets);
    $tmp = array_shift($sets);
    foreach ($tmp as $t) {
        if ($ks==1) {
            $results[] = $x.$t;
        }
        else combys($sets, $results, $x.$t);
    }
}

$results = array();

combys($sets, $results, ''); // call the function to generate the combinations

$str = join(' ', $results);
echo '<pre>' . wordwrap($str, 80) . '</pre>'; // output results



Or you can use a series of nested foreach loops after rearranging the array

foreach ($sets[0] as  $v0) {
    foreach ($sets[1] as $v1) {
        foreach ($sets[2] as $v2) {
            foreach ($sets[3] as $v3) {
                $results[] =  $v0.$v1.$v2.$v3;
            }
        }
    }
}  

Results:

1111 1112 1113 1114 1121 1122 1123 1124 1131 1132 1133 1134 1141 1142 1143 1144
1211 1212 1213 1214 1221 1222 1223 1224 1231 1232 1233 1234 1241 1242 1243 1244
1311 1312 1313 1314 1321 1322 1323 1324 1331 1332 1333 1334 1341 1342 1343 1344
1411 1412 1413 1414 1421 1422 1423 1424 1431 1432 1433 1434 1441 1442 1443 1444
2111 2112 2113 2114 2121 2122 2123 2124 2131 2132 2133 2134 2141 2142 2143 2144
2211 2212 2213 2214 2221 2222 2223 2224 2231 2232 2233 2234 2241 2242 2243 2244
2311 2312 2313 2314 2321 2322 2323 2324 2331 2332 2333 2334 2341 2342 2343 2344
2411 2412 2413 2414 2421 2422 2423 2424 2431 2432 2433 2434 2441 2442 2443 2444
3111 3112 3113 3114 3121 3122 3123 3124 3131 3132 3133 3134 3141 3142 3143 3144
3211 3212 3213 3214 3221 3222 3223 3224 3231 3232 3233 3234 3241 3242 3243 3244
3311 3312 3313 3314 3321 3322 3323 3324 3331 3332 3333 3334 3341 3342 3343 3344
3411 3412 3413 3414 3421 3422 3423 3424 3431 3432 3433 3434 3441 3442 3443 3444
4111 4112 4113 4114 4121 4122 4123 4124 4131 4132 4133 4134 4141 4142 4143 4144
4211 4212 4213 4214 4221 4222 4223 4224 4231 4232 4233 4234 4241 4242 4243 4244
4311 4312 4313 4314 4321 4322 4323 4324 4331 4332 4333 4334 4341 4342 4343 4344
4411 4412 4413 4414 4421 4422 4423 4424 4431 4432 4433 4434 4441 4442 4443 4444
Edited by Barand
  • Like 1
Link to comment
Share on other sites


function multi_array_permutations($arrays)
{
$loops = 1;
foreach ($arrays as $array)
{
$loops *= count($array);
}
for ($i = 0; $i < $loops; $i++)
{
$product = 1;
for ($j = 0; $j < count($arrays); $j++)
{
$product *= count($arrays[$j]);
print($arrays[$j][$i/($loops/$product) % count($arrays[$j])]);
}
print(PHP_EOL);
}
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.