3

I want to access and display one value from each of the arrays (which have the same index) using a randomly generated index.

My code:

$userRank = array(
    "Nightowl",
    "Demon Hunter",
    "Shadow Walker",
    "Legend"
);

$userLevel = array(
    "Level 5",
    "Level 10",
    "Level 15",
    "Level 20",
);

$deck = array();    
foreach($userRank as $rank){
    foreach($userLevel as $level){
        $deck[] = array("level" => $level, "rank" => $rank);
    }
}

shuffle($deck);
$getVal = array_shift($deck);

echo $getVal['level'] . ' ' . $getVal['rank'];

The end result of this is that the array retrieves random values from the provided arrays.

e.g. Level 10 Nightowl or Level 20 Shadow Walker

edit: i'm already aware if you use multidimensional array you can just assign each to each other but I want to do it with 2 separate arrays.

0

2 Answers 2

2

Are you expecting something like this? Here we are using array_map and returning array of level and rank from the function.

Try this code snippet here

<?php
ini_set('display_errors', 1);
$userRank = array(
    "Nightowl",
    "Demon Hunter",
    "Shadow Walker",
    "Legend"
);

$userLevel = array(
    "Level 5",
    "Level 10",
    "Level 15",
    "Level 20",
);

$deck=array_map(function($uL,$uR){
    return array("level" => $uL, "rank" => $uR);//returning array with level and rank
},$userLevel,$userRank);

shuffle($deck);
$getVal = array_shift($deck);

echo $getVal['level'] . ' ' . $getVal['rank'];
Sign up to request clarification or add additional context in comments.

Comments

1

i'm already aware if you use multidimensional array you can just assign each to each other but I want to do it with 2 separate arrays.

It seems to me that the other answer does not satisfy your conditions. This task can be done much more simply in just two lines.

It is only wasteful/indirect programming practices to combine the arrays, shuffle the arrays, then modify the array (cut the first element off), when you just want to access a single element from each array using one random key/index -- this way, you may re-use the original (undamaged) arrays "down-script".

This is the most direct method to use:

Code: (Demo)

$rand_index=rand(0,sizeof($userRank)-1);  // generate a random key/index
echo "{$userLevel[$rand_index]} {$userRank[$rand_index]}";  // display the desired values

Possible Output:

Level 10 Demon Hunter

3 Comments

I'm going to give it to you because I like your answer better.
@Jordan Rightly so. Sahil will need to eventually learn to slow down and post higher quality answers rather than trying to rush them in 8 minutes without fully comprehending the question. Good on you. I feel only by losing the green tick will he learn to be less hasty.
Yes, I would hope most users would do so. I rather be given a answer that's the correct(and or better) way to do something over a solution that "just works".

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.