Dizzee15 Posted March 11, 2009 Share Posted March 11, 2009 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 . Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/ Share on other sites More sharing options...
Dizzee15 Posted March 11, 2009 Author Share Posted March 11, 2009 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 . Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-781928 Share on other sites More sharing options...
kickstart Posted March 11, 2009 Share Posted March 11, 2009 Hi Maybe $number = array(list($whatever), list($whatever2)); All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-781931 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 Hi Maybe $number = array(list($whatever), list($whatever2)); All the best Keith Something is wrong about "$number = array(list($whatever), list($whatever2));" it prints error - "unexpected ',', expecting '='. Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-782993 Share on other sites More sharing options...
waterssaz Posted March 12, 2009 Share Posted March 12, 2009 What exactly are you trying to achieve with this line of code?? $number = array($whatever, $whatever2); Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-782996 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 What exactly are you trying to achieve with this line of code?? $number = array($whatever, $whatever2); I am trying to achieve one number from "$whatever" or "$whatever2" ! Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783002 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 array_rand Is that what you are after? Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783004 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783011 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783015 Share on other sites More sharing options...
kickstart Posted March 12, 2009 Share Posted March 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783017 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783034 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 What exactly are you trying to do? Why are you not group the numbers in the proper arrays? Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783039 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 What exactly are you trying to do? Why are you not group the numbers in the proper arrays? Because there's no other way. The thing I want to do is hard to explain. So, is there any way like this ??? Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783051 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783066 Share on other sites More sharing options...
kickstart Posted March 12, 2009 Share Posted March 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783070 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783127 Share on other sites More sharing options...
premiso Posted March 12, 2009 Share Posted March 12, 2009 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]}."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783153 Share on other sites More sharing options...
sasa Posted March 12, 2009 Share Posted March 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783176 Share on other sites More sharing options...
Dizzee15 Posted March 12, 2009 Author Share Posted March 12, 2009 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 ! Quote Link to comment https://forums.phpfreaks.com/topic/148914-solved-array-question/#findComment-783195 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.