我遇到了一个数组
的问题 $myarray =array();
for($i=1;$i<=12;$i++){
$myarray =array(
'userId'=>10,
'sum'=>40);
echo"<pre>";print_r($myarray);
}
这是我的虚拟数组,只是想了解实际面临的问题,这个循环一直生成一个包含元素$userid
或$sum
的数组
运行上面的代码后,它alwayz显示相同的数组,因为我正在使用静态值,但在实时它的生成数组有不同的$userid
或$sum
喜欢这个
Array
(
[sum] => 0
[userId] => 1
)
Array
(
[sum] => 5
[userId] => 4
)
Array
(
[sum] => 30
[userId] => 5
)
Array
(
[sum] => 0
[userId] => 16
)
Array
(
[sum] => 0
[userId] => 17
)
Array
(
[sum] => 16
[userId] => 34
)
我想将所有这些数组合并到一个数组中,以便我可以轻松找到
来自内部循环和相应$sum
生成的所有这些数组的最大$userid
。
循环后的输出应为
'first winner $sum = 30 $userid =5 `
'second winner $sum = 16 $userid =34`
'first winner $sum = 5 $userid =4 `
请建议我如何做到这一点,因为所有不同的数组都带有循环
由于
答案 0 :(得分:1)
在每次迭代中,您都会覆盖基本数组内容。
$myarray = array();
for ($i = 1; $i <= 12; $i++) {
$myarray[] = array(
'userId' => rand(1, 100),
'sum' => rand(1, 100),
);
}
echo "<pre>"; var_dump($myarray);
$myarray
将包含:
Array(
[0] => ['userId' => 5, 'sum' => 45],
[1] => ['userId' => 1, 'sum' => 85],
...
[11] => ['userId' => 8, 'sum' => 41],
);
要按sum
键对该数组进行排序,请使用usort()
usort($myarray, function ($a, $b) {
return $a['sum'] - $b['sum'];
});
答案 1 :(得分:1)
你能试试吗?
$sumArray=array(
0 => array('userId' => 5, 'sum' => 45),
1 => array('userId' => 1, 'sum' => 85),
2 => array('userId' => 8, 'sum' => 41)
);
$dummyArray = array();
foreach ($sumArray as $sumKey => $sumValue)
{
$dummyArray[$sumKey] = $sumValue['sum'];
}
array_multisort($dummyArray, SORT_DESC, $sumArray);
$count=0;
foreach ($sumArray as $sumKey => $sumValue)
{
$count=$count+1;
$rank=convert_number_to_words($count);
echo $rank.' winner $sum='.$sumValue['sum'].'<br/>';
}
来自http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
的功能function convert_number_to_words($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'First',
2 => 'Second',
3 => 'Third',
4 => 'Fourth',
5 => 'fiveth',
6 => 'sixth',
7 => 'seventh',
8 => 'eighth',
9 => 'nineth',
10 => 'tenth',
11 => 'eleventh',
12 => 'twelvth',
13 => 'thirteenth',
14 => 'fourteenth',
15 => 'fifteenth',
16 => 'sixteenth',
17 => 'seventeenth',
18 => 'eighteenth',
19 => 'nineteenth',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}