0

私のconfig.php内容:

$a1="title";
$b1="text";
$a2="title2";
$b2="text2";

そして私は1.phpこれが含まれていますconfig.php

($a1 と $b1) または ($a と $b) をランダムに含める必要があります。

これどうやってするの?:)

ありがとうございました。

4

1 に答える 1

1

このデータが関連しており、ランダムなセットが必要な場合は、配列に格納します。

$sets = array(
    array(
        'title' => 'Some title',
        'text'  => 'Some text here about the title'
    ),
    array(
        'title' => 'Some other title',
        'text'  => 'Some other text here about the title'
    )
);

この配列には、選択できる 2 つのインデックスがあります。

$sets[0]; // Title: Some title, Text: Some text here about the title
$sets[1]; // Title: Some other title, Text: Some other text here about the title

これらのいずれかがランダムに必要な場合は、次のようにします。

$index = array_rand( $sets, 1 );

0これにより、 またはが選択されます1。配列内のエントリが増えると、この数が大きくなる可能性も高くなります。これを使用して、データ セットの 1 つを取得できます。

$dataSet = $sets[ $index ];

次に、出力を表示します。

echo $dataSet['title'];
echo $dataSet['text'];
于 2012-05-20T17:21:31.313 に答える