Jump to content

Recommended Posts

Here's the question :

$whatever = array(10, 20);
$whatever2 = array(30, 40);
$number = array($whatever, $whatever2);
shuffle($number);

Now if $number[0] is $whatever I want $number[0] to be 10. How to do it ? Should it be like that $number[0][1] ?

I hope you understand my question  :).

EDIT. Not $number[0][1], but $number[0][0] i wanted to say  :-[.

array_rand Is that what you are after?

Probalbe not, I don't know what it does. Perhaps you don't understand my question. After "$number" shuffles, what do I need to type if I need to get one number - 10 or 20 from "$whatever", or 30 or 40 from "$whatever2" it doesn't matter.

You could read the manual on it to find out.

 

<?php
$whatever = array(10, 20);
$whatever2 = array(30, 40);
$number = array_merge($whatever, $whatever2);

$randomKey = array_rand($number);

echo $number[$randomKey];
?>

 

As far as I can tell, that is what you are ultimately after.

 

Something is wrong about "$number = array(list($whatever), list($whatever2));" it prints error - "unexpected ',', expecting '='.

 

My mistake. Try $number = array_merge($whatever, $whatever2);

 

That should give you a single array to shuffle (although the keys might be a bit screwey if you are relying on them).

 

All the best

 

Keith

 

Something is wrong about "$number = array(list($whatever), list($whatever2));" it prints error - "unexpected ',', expecting '='.

 

My mistake. Try $number = array_merge($whatever, $whatever2);

 

That should give you a single array to shuffle (although the keys might be a bit screwey if you are relying on them).

 

All the best

 

Keith

But if I want only 10 or 30, not 20 and 40 ? Is there any way like this?

<?php
$whatever = array(10, 20);
$whatever2 = array(30, 40);
$number = array_merge($whatever, $whatever2);
$tenKey = array_search(10, $number);
$thirtyKey = array_search(30, $number);

echo "The 10 is at {$tenkey} and returns {$number[$tenKey]}<br />The 30 is at {$thirtyKey} and returns {$number[$tenkey]}";
?>

 

Without more information, that is what I gather that you want to return. But I am just guessing here as I cannot fully see what you are trying to do.

But if I want only 10 or 30, not 20 and 40 ? Is there any way like this?

 

My understanding was that you had 2 arrays of values, and you wanted a single random value from one or other of those 2 arrays. If so the code I gave you is to merge the 2 arrays into 1 so that you can shuffle that single array.

 

All the best

 

Keith

As I can see none of you understands my question. Okay, here's full script :

$number = array(10, 20);
$number2 = array(30, 40);
$number3 = array(50, 60);

$numbers = array( "$number", "$number2", "$number3");
shuffle($numbers);

$number4 = 25;

if ($number4 >= $??????1) { // '$??????1' = 10 or 30 or 50
  $result = $number4 - $??????1;
  $result2 = $number4 - $??????2; // '$??????2' = 20 or 40 or 60 
} else {
while ($number4 < $1??????) {
	shuffle($numbers);
}
$result = $number4 - $??????1;
$result2 = $number4 - $??????2;
}

Now u get it ????? I don't know other way how to explain.

Well, I am still very confused. Why have multiple arrays like that? Why shuffle like you are shuffleing ???

 

How about this, given the code you posted above, what do you expect $result and $result2 to be after it has ran....

 

EDIT:

Not sure if this is what you want, but maybe it is:

 

<?php
$numbers = array(10, 20, 30, 40, 50, 60);

$number = 25;

$rounded = ($number / 10);

$nearest = ($number - $rounded);

$key = array_search($nearest);

echo "The nearest to {$number} is {$nearest} which returned a key of {$key} which is the value {$numbers[$key]}.";
?>

try

$number = array(10, 20);
$number2 = array(30, 40);
$number3 = array(50, 60);

$numbers = array( $number, $number2, $number3);
shuffle($numbers);

$number4 = 25;

//if ($number4 >= $??????1) { // '$??????1' = 10 or 30 or 50
//  $result = $number4 - $??????1;
//  $result2 = $number4 - $??????2; // '$??????2' = 20 or 40 or 60 
//} else {
while ($number4 < $numbers[0][0]) {
	shuffle($numbers);
}
$result = $number4 - $numbers[0][0];
$result2 = $number4 - $numbers[0][1];
//}

when you setup array $numbers remove quotes

try

$number = array(10, 20);
$number2 = array(30, 40);
$number3 = array(50, 60);

$numbers = array( $number, $number2, $number3);
shuffle($numbers);

$number4 = 25;

//if ($number4 >= $??????1) { // '$??????1' = 10 or 30 or 50
//  $result = $number4 - $??????1;
//  $result2 = $number4 - $??????2; // '$??????2' = 20 or 40 or 60 
//} else {
while ($number4 < $numbers[0][0]) {
	shuffle($numbers);
}
$result = $number4 - $numbers[0][0];
$result2 = $number4 - $numbers[0][1];
//}

when you setup array $numbers remove quotes

GODLIKE ! THANK YOU ! It works like I wanted to !

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.