---
# System prepended metadata

title: 資料結構處理 迴圈印出
tags: [Laravel]

---

---
tags: Laravel
---


# 資料結構處理 迴圈印出

### ==基本處理==
- 運用foreach將陣列內容印出
```php=

$paple = ['bryan',918,'make'];

 // $key 與 $getpeple 都可制定
 foreach ($paple as $key => $getpeple) {

    echo('這位是' . $getpeple ."\r\n" );
}
```
### ==陣列包陣列==
- 常見的陣列中包陣列處理

```php=
$paple = [
    ['name'=>'bryan','grade'=>38],
    ['name'=>'make','grade'=>20],
];

foreach ($paple as $key => $arrarList) {
    # code...
    echo('這是第'. $key .'的'.  $arrarList["name"] ."\r\n" );
}

//印出

這是第0的bryan
這是第1的make


```

### ==資料加總印出==

```php=
$coco= [ 20,30,50,60];
// 定義一個初始值 0
$getcoco = 0;

foreach ($coco as $key => $totolist) {
   // 每次 $getcoco = $totolist +  $totolist
    $getcoco +=  $totolist;
}

echo($getcoco);  // 印出 160
echo($getcoco/5);// 印出 16032
```

### ==if 、 isset函式判斷是否存在==

```php=
// 資料結構
$man = ["bryan", "mars", "saso", "lolo5"];

$bonus = [
    'bryan' => 2,
    'mars' => 1.2,
    'saso' => 3,
];

foreach ($man as $key => $manlist) {

     //邏輯判斷
     //如果 $man的名稱有存在 $bonus名單中就印出
    if (isset($bonus[$manlist])) {

        echo (($bonus[$manlist] * 2000) . "\r\n");
    } else {
        echo ("查無此資料");
    }
}
```