1

このPHP変数のコンテンツは、ページが更新またはアクセスされるたびにランダムな順序になります。

そのような

$test = 'a, b, c, d, e, f';

ランダムに取得するための最良の方法が欲しい、例

$test = 'c, b, d, f, a, e';

$test = 'd, e, c, a, f, b';

解決策はありますか?

4

2 に答える 2

6
$test = 'a, b, c, d, e, f';
$testArray = explode(', ',$test); //explode to array

shuffle($testArray); //shuffle it up

echo implode(', ', $testArray); //show the shuffledlyness
于 2012-04-11T20:08:59.083 に答える
4

私はこのようなものを提案します:

$content = 'a, b, c, d, e, f';
$content = explode(', ', $content);

shuffle($content);

$content = implode(', ', $content);

echo $content;

このコードの機能:

  • explode、、などaの項目で配列を作成します。bc
  • shuffle配列をシャッフルします
  • implode各アイテムの間に置くと、元のランダム化された文字列が返されます
于 2012-04-11T20:10:25.690 に答える