11Tami Posted January 23, 2008 Share Posted January 23, 2008 Hello, how would I pick an item from these 2 fields randomly, instead of one at a time? For example this picks an item randomly from both $one1 $one2 separately. How can I choose randomly from both of them instead? So that I only have one result at the end instead of 2? Please let me know, thank you very much. <?php $one= "<span style='color: rgb(70,0,0)'>test1</span>"; $one[3] = "file1.txt"; $one1 = preg_grep('/(?:<span)/', $one); $one2 = preg_grep('/(?:<span)/', $one, PREG_GREP_INVERT); $get1 = $one1[array_rand($one1)]; $get2 = $one2[array_rand($one2)]; $get3 = file_get_contents($get2); echo $get1; echo $get3; ?> Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/ Share on other sites More sharing options...
priti Posted January 23, 2008 Share Posted January 23, 2008 hi, why cann't you use array_rand() again . $araay=array($get1,$get2); $final_output=array_rand($araay,1); array_rand() returns the key for a random entry. Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-446664 Share on other sites More sharing options...
pdkv2 Posted January 23, 2008 Share Posted January 23, 2008 i suggest to use $arr_result=array_merge($one1,$one2); and use the $arr_result to choose randomly from both of them Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-446708 Share on other sites More sharing options...
11Tami Posted January 23, 2008 Author Share Posted January 23, 2008 Thanks both of you. Priti, what does the 1 do here? I dont know how to try it. ($araay,1); I also tried these, this returns the file file1.txt on every page load correctly and never shows the text "test1". <?php $one[1] = "<span style='color: rgb(70,0,0)'>test1</span>"; $one[2] = "file1.txt"; $two1 = preg_grep('/(?:<span)/', $one); $two2 = preg_grep('/(?:<span)/', $one, PREG_GREP_INVERT); $rand1 = $two1[array_rand($two1)]; $rand2 = $two2[array_rand($two2)]; $file = file_get_contents($rand2); $get = $rand1 + $file; echo $get; ?> .....This only brings back the word "Array" on every page load and nothing else. Is there anyway I can grab onto both? Please let me know, thank you very much. <?php $one[1] = "<span style='color: rgb(70,0,0)'>test1</span>"; $one[2] = "file1.txt"; $two1 = preg_grep('/(?:<span)/', $one); $two2 = preg_grep('/(?:<span)/', $one, PREG_GREP_INVERT); $rand1 = $two1[array_rand($two1)]; $rand2 = $two2[array_rand($two2)]; $file = file_get_contents($rand2); $get = array_merge($rand1,$file); echo $get; ?> Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-447218 Share on other sites More sharing options...
priti Posted January 24, 2008 Share Posted January 24, 2008 Hi, ($araay,1); here 1 specifies how many entries you want to pick.$araay is array and from this array i am trying to pick randomly any one entery. for more details for this function read http://in2.php.net/array_rand line. Regards Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-447526 Share on other sites More sharing options...
pdkv2 Posted January 24, 2008 Share Posted January 24, 2008 In the code snippet you wrote echo $get; but $get is an array, you can not echo on an arrayname. i guess you need write this $final_output=array_rand($get,1); echo $final_output; rgds Sharad Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-447547 Share on other sites More sharing options...
11Tami Posted January 24, 2008 Author Share Posted January 24, 2008 Thanks a lot you guys, pdkv you lost me that time. The only $get I see is at the end, the arrays are named something else aren't they? Please let me know, thank you very much. Link to comment https://forums.phpfreaks.com/topic/87320-looking-through-two-fields-for-a-random-item/#findComment-447586 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.