2

This code is supposed to created a multidimensional array, shuffle it, select the first array, and pass the four values to four different variables called $common, $uncommon, $rare, and $superrare.

<?php

//create multidimentional array
$distribution = array
(
array(115, 50, 15, 0),
array(115, 49, 16, 0),
array(115, 48, 17, 0),
array(116, 49, 15, 0),
array(116, 48, 16, 0),
array(116, 47, 17, 0),
array(117, 48, 15, 0),
array(117, 47, 16, 0),
array(117, 46, 17, 0),
array(118, 47, 15, 0),
array(118, 46, 16, 0),
array(118, 45, 17, 0),
array(119, 46, 15, 0),
array(119, 45, 16, 0),
array(119, 44, 17, 0),
array(120, 45, 15, 0),
array(120, 44, 16, 0),
array(120, 43, 17, 0),
);

//shuffle the md-array
shuffle($distribution);

//select first array from $distribution
$slice[] = array_slice($distribution ,0, 1);

//create an array for key values
$cardrarity = array('common', 'uncommon', 'rare', 'superrare');

//combine $cardrarity and $slice
$cardraritycount = array_combine($cardrarity, $slice);

//create a variable for each value in 
foreach ($cardraritycount as $key => $value){
    $$key = $value;
    }
?>

When I view the contents of $slice it looks like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 120
                    [1] => 43
                    [2] => 17
                    [3] => 0
                )

        )

)

When I try and combine $slice with $cardrarity, I get no output when I try and echo the contents. I think my problem is when I'm doing the array slice. Any help would be appreciated.

3
  • is this a typo with 2 $ here: $$key = $value;? Commented Apr 29, 2016 at 18:34
  • 1
    @JeffPuckettII: No they are attempting variables variables. Commented Apr 29, 2016 at 18:45
  • @AbraCadaver, wow thanks! just found the doc and I had no idea of such a thing Commented Apr 29, 2016 at 18:47

3 Answers 3

5

There's no need to complicate such simple things. Simply do this:

// Shuffle the array
shuffle($distribution);

// Assign values to the respective variables
list($common, $uncommon, $rare, $superrare) = $distribution[0];

// Display values
echo $common . "<br />";
echo $uncommon . "<br />";
echo $rare . "<br />";
echo $superrare . "<br />";

Here's the reference:

Sign up to request clarification or add additional context in comments.

1 Comment

That did it. Thank you so much. I'm just teaching myself PHP, wasn't aware of list. Struggled with this one all day.
4

array_slice($distribution, 0, 1) returns a multi-dimensional array. It's a 1-element array whose first element is the first element of $distribution. If you just want the first element of $distribution, use $distribution[0].

To assign to multiple variables from an array, use an assignment to the list() function.

So the whole thing should be:

list($common, $uncommon, $rare, $superrare) = $distribution[0];

2 Comments

Thank you, that worked. Rajdeep must have been a split second faster in answering.
But he didn't explain what was wrong with your code like I did.
1

Not only is array_slice() returning a multi-dimensional array but you are creating even another dimension with $slice[].

Using $distribution[0] you have two options other than list():

shuffle($distribution);
$cardrarity = array('common', 'uncommon', 'rare', 'superrare');
$cardraritycount = array_combine($cardrarity, $distribution[0]);

Then your foreach() works correctly:

foreach ($cardraritycount as $key => $value){
    $$key = $value;
}

Or extract() the array as variables:

extract($cardraritycount);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.