[英]Get specific values from randomized multidimensional array with url key
我不放棄如何從特定鍵號上分別回顯這兩個項目。 最終,我希望能夠保留當前的隨機函數,但能夠在需要時通過諸如index.php?key=3
類的URL從數組中獲取一組特定的值–假設訪問者隨機進行了“第三rzU_fLcxIN0”,並想與某人分享,然后繼續查看具有相應值的url index.php?key=3
,並能夠通過使用按鈕將新值隨機化來繼續。
也許我問了很多,但是一旦理解了要使用的部分(以及如何使用),感覺這應該並不復雜。 我對此並不陌生,因此所有有關如何解決此類問題的幫助和建議對我都非常有用!
更新我將嘗試更好地解釋隨機與非隨機問題。 標准行為是向訪問者顯示隨機內容,但訪問者可以與他人共享特定帖子。 想象一下當“訪問者”訪問該站點時的以下步驟;
index.php?key=3
( 示例 )。 key=3
的內容。 index.php
<a class="randomizerButton" data-href="data.php">Randomize</a>
<hr>
<div id="results">
<?php include('data.php'); ?>
</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('a.randomizerButton').click(function(){
scriptUrl = $(this).attr('data-href');
$.post(scriptUrl, function(response){
$('#results').html(response);
});
});
});
</script>
data.php
<?php
$var = array(
//1,2,3,4... etc. are unique IDs
1 => array("First", "0wLljngvrpw"),
2 => array("Second", "TINASKjjNfw"),
3 => array("Third", "rzU_fLcxIN0"),
4 => array("Fourth", "aZZ8UnCTVJA"),
);
$finalVar = $var[array_rand($var)];
echo $finalVar[0].'<br/>'.$finalVar[1];
echo ('<hr>');
// I've started out with things like the one below but I don't know how to output the values from a multidimensional array,
// Didn't manage to find a helpful resource on the problem, mainly since I don't know if this is the correct way to approach this.
$key='2';
echo $var[$key]; //outputs Array, need to go one level deeper I guess?
// Any pointers on how to load this specific value while maintaining the random button function as well, I'd be very grateful.
// $_GET["key"]...?
// loop array...?
?>
為了更好地解釋我的評論,對於固定與隨機,這是一個邏輯條件,為此,我們使用if語句。
假設您有url index.php?key=3
,我們只是在$_GET
檢查
if( isset( $_GET['key'] ) && isset( $var[$_GET['key']] ) ){
$finalVar = $var[$_GET['key']];
} else {
$finalVar = $var[array_rand($var)];
}
當有人將輸入隨機化時,
header('location: index.php');
exit();
清除網址。 或從Javascript
window.location.href = 'index.php';
這將在條件條件的if
和else
部分執行時失敗,它將使用隨機鍵訪問數組,否則,如果該key
位於url查詢和$var
數組中,則它們將獲得該項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.