我开始学习数组并且他们非常困惑。我想用这个生成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>";
}
这恰好与4个数字相呼应4次。我需要&#34;首先&#34;成为第一个数字,&#34;第二个&#34;成为第四个和第四个相同的第二个数字。我一直在网上搜索,但并不知道要搜索什么来找到答案。
如果有人回答他们,请尽可能多地详细说明某些功能的作用,我想学习的不仅仅是获取工作代码:)
答案 0 :(得分:2)
这是一个完整的代码,试试吧
$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
答案 1 :(得分:2)
如果我的理解是正确的,你有一个数组,你想要它内的某些值吗?
那么为什么不使用array keys:
$array = array('peach','pear','apple','orange','banana');
echo $array[0]; // peach
echo $array[1]; // pear
echo $array[2]; // apple
或者你可以loop through the array这样:
foreach ($array as $arrayKey => $arrayValue) {
// First value in the array is now below
echo $arrayKey; // 0
echo $arrayValue; // peach
}
您还可以检查值in an array是否如此:
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'
)
答案 2 :(得分:2)
您可以使用sizeof()
它是数组大小的返回大小,并手动传递键值。喜欢-
echo $numbers[0];
echo $numbers[1];
echo $numbers[2];
答案 3 :(得分:1)
试试这个:
<?php
$numbers = range(1,4);
shuffle($numbers);
$test = array();
foreach($numbers as $k=>$v){
$test[$k] = $v;
}
echo "<pre>";
print_r($test);
?>
这将输出为:
Array
(
[0] => 3
[1] => 4
[2] => 2
[3] => 1
)
之后你可以这样做:
$myvar["first"] = $test[0];
$myvar["second"] = $test[1];
$myvar["third"] = $test[2];
$myvar["fourth"] = $test[3];
每个数组都有一个键值对,如果你有一个数组说:
$myarr = array("a", "b", "c");
然后你可以访问价值&#34; a&#34;如$myarr[0]
,&#34; b&#34;如$myarr[1]
等等。
在for循环中,我使用它们的键和值循环遍历数组,这不仅会为您提供数组的键,还会提供与该键相关的值。
修改强> 改进Luthando Loot answered:
<?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
?>
<强>输出:强>
Array
(
[first] => 4
[second] => 3
[third] => 2
[fourth] => 1
)
答案 4 :(得分:1)
使用这种方式
$numbers[0];
$numbers[1];
$numbers[2];
$numbers[3];
答案 5 :(得分:1)
首先将一组键设为
$key = ['first','second','third','fourth'];
然后你可以简单地使用array_combine
作为
$numbers = range(1,4);
shuffle($numbers);
$key = ['first','second','third','fourth'];
$result = array_combine($key,$numbers);