Forum Moderators: coopster

Message Too Old, No Replies

calling two arrays with array rand?

         

Simone100

11:16 pm on Jan 22, 2008 (gmt 0)

10+ Year Member



Hello, how would you get PHP to pick an item randomly from 2 array variables instead of one? For example this picks an item randomly from both $one1 $one2 separately. How do I get it to pick an item randomly from both of them at the same time? 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;
?>

[1][edited by: Simone100 at 11:16 pm (utc) on Jan. 22, 2008]

coopster

9:25 am on Jan 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you are picking only one entry, array_rand [php.net] returns the key for a random entry. Assign that value to a variable and use it as your index.

Simone100

9:20 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Thanks a lot, you mean like this? This only 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;
?>

coopster

9:28 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry but I really have no idea what you are trying to do ;)
Perhaps you can explain in just plain words, not programming code, what it is you are trying to accomplish.

Simone100

10:01 pm on Jan 23, 2008 (gmt 0)

10+ Year Member



Sorry about that. I'm trying to have the script look through everything in array $one for both span text and web page text files.

For it to choose one randomly. Problem I'm having is for the web page files file_get_contents needs to be used somewhere or the web page files aren't echo'd properly. But when I use file_get_contents
then the text isn't echo's properly. How can I include them both without getting php error messages? Please let me know, thank you very much.

PHP_Chimp

11:00 pm on Jan 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$one[1] = "<span style='color: rgb(70,0,0)'>test1</span>";
$one[2] = get_file_contents("file1.txt");

The rest of your code should be able to stay the same.
$two1 = preg_grep('/(?:<span)/', $one);
$two2 = preg_grep('/(?:<span)/', $one, PREG_GREP_INVERT);

Although the $two1 will get you all parts that match your grep pattern, $two2 will get you everything that doesnt match your grep pattern. So between them you should have every element of the $one array in there.

Its late so maybe I've missed something, but it looks like you are going around in circles.

. I'm trying to have the script look through everything in array $one for both span text and web page text files.
For it to choose one randomly


<?php
$one[1] = "<span style='color: rgb(70,0,0)'>test1</span>";
$one[2] = file_get_contents("file1.txt"); // returns a string of the file contents of file1.txt
$search = preg_grep('/(?:<span)/', $one); // matching pattern chosen.
$rand = array_rand($search); // will pick 1 random [b]key[/b]
echo $search[$rand];
?>

Simone100

12:09 am on Jan 24, 2008 (gmt 0)

10+ Year Member



Thanks a lot. I must be missing something still, not sure what youre trying to tell me.

If I do this:

<?php
$one[1] = "<span style='color: rgb(70,0,0)'>test1</span>";
$one[2] = file_get_contents("file1.txt");
$search = preg_grep('/(?:<span)¦(?:txt)/', $one);
$rand = array_rand($search);
echo $search[$rand];
?>

It shows "test1" on every page load.
How do I get it to grab "file1.txt" as well?

Please let me know, thank you very much.

PHP_Chimp

7:28 pm on Jan 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The file_get_contents("file1.txt"); will return a string of the contents of the file1.txt.
So you will still have 2 members of the $one array, just the second value will probably be a lot longer than the first.

So you should have a 50% chance of getting either the text from $oneor $one[2].

I am assuming that you have a <span tag in the contents of file1.txt, or 'txt' in there? As that is all your regex is searching for.

<edit>
You could try -


<?php
$one[0] = "<span style='color: rgb(70,0,0)'>test1</span>";
$one[1] = file_get_contents("file1.txt");
$rand = array_rand($one);
$result = preg_match_all('/(<span [^\n]+)¦(txt [^\n]+)/', $one[$rand], $matches); // you may well need to alter this to get what you want.
if ($result!== false) {
echo '<pre>;
print_r($matches);
echo '</pre>';
}
else {
echo 'There was an error, please try again ;)';
}
?>

[1][edited by: PHP_Chimp at 7:35 pm (utc) on Jan. 24, 2008]

Simone100

2:10 am on Jan 25, 2008 (gmt 0)

10+ Year Member



Thanks a lot, I think I got it with preg_grep, I'll do some testing and send you a sticky or others stay tuned....

[edited by: Simone100 at 2:36 am (utc) on Jan. 25, 2008]

Simone100

3:41 am on Jan 25, 2008 (gmt 0)

10+ Year Member



I wanted to stick with preg_grep so I put the txt files in another array to only appear on every other page load. I got tired of trying to include both in one echo. It'll get me by. Thanks for the great help.