Php 如何查找数组的第一个、第二个、第三个等数字

Php 如何查找数组的第一个、第二个、第三个等数字,php,arrays,Php,Arrays,我已经开始学习数组,它们非常令人困惑。我想使用以下方法生成4个数字: $numbers = range(1,4); 然后我用这个洗牌: shuffle($numbers); 现在我想把每个数字作为一个变量,我被告知最好的方法是数组。我有以下代码: foreach($numbers as $number){ $test = array("first"=>"$number"); echo $test['first']; } 这样做的目的是将所有4个数字一起回显,如“3142”或“32

我已经开始学习数组,它们非常令人困惑。我想使用以下方法生成4个数字:

$numbers = range(1,4);
然后我用这个洗牌:

shuffle($numbers);
现在我想把每个数字作为一个变量,我被告知最好的方法是数组。我有以下代码:

foreach($numbers as $number){

$test = array("first"=>"$number");
echo $test['first'];

}
这样做的目的是将所有4个数字一起回显,如“3142”或“3241”等,这接近我想要的,但我需要所有4个数字都有各自的变量。所以我做了这个:

foreach($numbers as $number){

$test =     array("first"=>"$number","second"=>"$number","third"=>"$number","fourth"=>"$number");
echo $test['first']," ",$test['second']," ",$test['third']," ",$test['fourth'],"     <br>";

}
foreach($number作为$number){
$test=数组(“第一个”=>“$number”,“第二个”=>“$number”,“第三个”=>“$number”,“第四个”=>“$number”);
echo$test['first']、“”、$test['second']、“”、$test['third']、“”、$test['fourth']、“
”; }
这只是4个数字的4次重复。我需要“first”作为第一个数字,“second”作为4的第二个数字,第三个和第四个数字相同。我一直在网上搜索,但不知道具体要搜索什么才能找到答案


如果有人回答,请他们尽可能详细地介绍某些函数的功能,我想学习的不仅仅是获取工作代码:)

这是完整的代码,请尝试

$numbers = range(1,4); //generate four numbers
shuffle($numbers);   //shuffle them

$test =  array(); //create array for the results
$words = array("1st", "2nd", "3rd", "4th","5th"); //create your array keys
$i=0;
foreach($numbers as $number){
   $test[] = array($words[$i]=>$number);  //add your array keys & values 
    $i++;
}
print_r($test); //show your results
试试这个:

<?php
$numbers = range(1,4);
shuffle($numbers);
$test = array();

foreach($numbers as $k=>$v){
    $test[$k] = $v;
}

echo "<pre>";
print_r($test);
?>
之后,您可以执行以下操作:

$myarr = array("a", "b", "c");
每个数组都有一个键、值对,如果您有一个数组,请说:

<?php
$numbers = range(1,4);//generate four numbers
shuffle($numbers);//shuffle them

$test  =  array();//create array for the results
$words = array("first", "second", "third", "fourth"); //create your array keys
$i = 0;
foreach($numbers as $number){
   $test[$words[$i]] = $number;//add your array keys & values 
    $i++;
}
echo "<pre>";
print_r($test); //show your results
?>
然后您可以访问值“a”作为
$myarr[0]
,“b”作为
$myarr[1]
,依此类推

在for循环中,我使用它们的键和值在数组中循环,这不仅会给出数组的键,还会给出与该键关联的值

编辑: 改进什么:

这样

$numbers = range(1,4);
shuffle($numbers);
$key = ['first','second','third','fourth'];
$result = array_combine($key,$numbers);

首先制作一个键数组,如下所示:

$array = array('peach','pear','apple','orange','banana');
echo $array[0]; // peach
echo $array[1]; // pear
echo $array[2]; // apple
然后你可以简单地使用


如果我的理解是正确的,那么您有一个数组,并且希望从中获得某些值

那么为什么不直接使用:

或者你也可以这样:

您还可以检查值是否如下所示:

编辑:

您可以使用
sizeof()
它是数组大小的返回大小,并手动传递键值。像-
echo$number[0];
echo$数字[1];

echo$数字[2]

如果您手工编写整个数组
$test
,您甚至不需要foreach循环。只需编写数组(“第一个”=>$numbers[0],“第二个”=>$numbers[1]…)。另外,在echo语句中,将
替换为
,因为您不能将字符串与
@treegarden连接起来。很抱歉,我理解最后一点,我一直使用逗号:s,非常有效,谢谢。你能再解释一下吗?我不明白$k= > $v位,空白数组如何链接成为数字:SThanks Criesto,而且这个链接比我使用的W3Studio好得多!我想要的是每个数字都有自己的变量:)你的答案看起来像是我手动将每个数字放入数组中,但数字的顺序每次都会改变(这是必需的),所以这不起作用?@Ben你只需要在数组中循环并存储前3个值。Criesto已经这样做了,但我会给你另一个选项,让你进一步了解。使用“arrayKey”和“arrayValue”标记非常有用,我现在了解得更多了。谢谢:)
Array
(
    [first] => 4
    [second] => 3
    [third] => 2
    [fourth] => 1
)
$numbers[0];
$numbers[1];
$numbers[2];
$numbers[3];
$key = ['first','second','third','fourth'];
$numbers = range(1,4);
shuffle($numbers);
$key = ['first','second','third','fourth'];
$result = array_combine($key,$numbers);
$array = array('peach','pear','apple','orange','banana');
echo $array[0]; // peach
echo $array[1]; // pear
echo $array[2]; // apple
foreach ($array as $arrayKey => $arrayValue) {
  // First value in the array is now below
  echo $arrayKey; // 0
  echo $arrayValue; // peach
}
if (in_array('orange', $array)) {
  echo 'yes';
}
// Our array values
$array = array('peach','pear','apple','orange','banana');
// We won't shuffle for the example, we need expected results
#$array = shuffle($array);
// We want the first 3 values of the array
$keysRequired = array(0,1,2);
// This will hold our results
$storageArray = array();
// So for the first iteration of the loop $arrayKey is going to be 0
foreach ($array as $arrayKey => $arrayValue) {
  // If the array key matches one of the values in the required array 
  if (in_array($arrayKey, $keysRequired)) {
    // Store it within the storage array so we know what value it is
    $storageArray[] = $arrayValue;
  }  
}
// Let's see what values have been stored
echo "<pre>";
print_r($storageArray);
echo "</pre>";
Array
(
    [0] => 'peach'
    [1] => 'pear'
    [2] => 'apple'
)