# 求出一個數組的最大值,并得到對應的數組下標
> 思路
> 1. 定義一個數組
> 2. 將第一個數默認為最大值
> 3. 從第二個數開始進行循環比較,發現有更大的就進行交換
## Go語言實現
```go
// 求出一個數組的最大值,并得到對應的下標
var intArr [5]int = [...]int{1, -1, 9, 90, 11}
maxVal := intArr[0]
maxValIndex := 0
for i := 1; i < len(intArr); i++ {
// 開始從第二個數開始比較,如果發現有更大的就進行交換
if maxVal < intArr[i] {
maxVal = intArr[i]
maxValIndex = i
}
}
fmt.Printf("maxVal=%v maxValIndex=%v", maxVal, maxValIndex)
```
## PHP實現
```php
class WArray
{
protected $array = [];
public function __construct($array)
{
$this->array = $array;
}
public function getArrayMaxValueAndIndex()
{
$maxVal = $this->array[0];
$maxValIndex = 0;
foreach ($this->array as $k => $v) {
if ($maxVal < $this->array[$k]) {
$maxVal = $this->array[$k];
$maxValIndex = $k;
}
}
return json_encode([
'maxVal' => $maxVal,
'maxValIndex' => $maxValIndex,
]);
}
}
```
**調用結果**
```php
// 調用
$warray = new WArray([1, -1, 9, 90, 11]);
$arrayMaxValueAndIndex = $warray->getArrayMaxValueAndIndex();
dd($arrayMaxValueAndIndex);
// 這里的dd是一個打印函數
// 結果顯示
{"maxVal":90,"maxValIndex":3}
```
- PHP獲取客戶端瀏覽器信息和版本
- PHP獲取客戶端操作系統信息
- 無限級分類
- git使用
- 權限檢測思路
- Vue學習
- 遇到的一些問題
- PHP的編碼思維和技巧
- mysql復習
- tp5
- ThinkPHP5.x 公共函數
- TP5登錄注冊
- TP5使用模板繼承
- ThinkPHP5.1 清除緩存
- thinkphp5實現安裝程序
- 安全
- tp中實現跨域代碼
- ThinkPHP5.1配合pjax實現菜單欄無刷新跳轉
- 獲取數據庫版本和數據庫大小
- 模型的基本CURD操作
- 商品spu
- 全局異常處理類
- ExceptionHandler
- BaseException
- PHP函數之error_reporting(E_ALL ^ E_NOTICE)詳細說明
- 微信小程序
- wx:for
- tp6
- 分離的一些模塊
- session開啟
- Spring
- 依賴注入
- 數據結構
- 二叉樹
- js獲取地址欄變量
- PHP設計模式
- 面向對象
- PHP1
- PHP性能優化
- Java學習
- static關鍵字
- 多態
- 接口、階乘
- 大佬給的面試題
- 訪問量為5000萬的博客系統設計
- PHP可變參數
- Nginx的配置案例
- 求數組中的最大值,并返回數組索引
- PHP面試方向
- PHP數組工具類ArrUtil
- 字符串工具類StrUtil
- PHP使用curl發送請求
- mysql
- PHP上傳base64圖片處理函數
- webstorm小程序常用配置
- 郵箱正則表達式
- leetcode mysql記錄
- 函數庫