as the subject says, i've built a multidimensional array, i now want to get the first and second element from a random array item, this is what I have.

any help appreciated.

<?php
require '../db.php';
$result = mysqli_query($link2, "SELECT * FROM `rss_news` WHERE IconID = 0");	
while ($row = $result->fetch_assoc()){
	$ID = $row['ID'];

$Options = array();
$Header = mysqli_escape_string($link2, $row['Headline']);
echo $Header . "<br>";
	$result2 = mysqli_query($link2, "SELECT * FROM `keywords` ");	
	while ($row2 = $result2->fetch_assoc()){
		if (strpos($Header, trim($row2['Word'])) !== false){
			array_push($Options, array($row2['IconID'], $row2['Word']));
		}
	}

var_dump($Options);
echo "<br>";
if (!empty($Options)){
	$r = array_rand($Options, count($Options));
	echo $Options[$r[0]];
	#echo $Options[array_rand($Options[1])];
	#$IconID = 
	#$result = mysqli_query($link2, "UPDATE `rss_news` SET IconID = $IonID WHERE ID = $ID");
}
echo "<hr>";
}
?>

    ...and what's the problem you're having?

      Weedpacket;11064457 wrote:

      ...and what's the problem you're having?

      I can't get either value.

        If you only need one random element from $Options, I would omit the second parameter to array rand so that you just get a single key. Otherwise, in cases where it only has one element, it will return just the one key, not an array of multiple keys.

                $key = array_rand($Options);
                $iconID = $Options[$key][0];
                $word = $Options[$key][1];
        

        Or if you prefer one-liners:

                list($iconID, $word) = $Options[array_rand($Options)];
        
          NogDog;11064461 wrote:

          If you only need one random element from $Options, I would omit the second parameter to array rand so that you just get a single key. Otherwise, in cases where it only has one element, it will return just the one key, not an array of multiple keys.

                  $key = array_rand($Options);
                  $iconID = $Options[$key][0];
                  $word = $Options[$key][1];
          

          Or if you prefer one-liners:

                  list($iconID, $word) = $Options[array_rand($Options)];
          

          So simple, why do I complicate things in my head...

            Write a Reply...