1. 遍历循环输出
1.1. volist(错误) 与 foreach(正确)输出一维数组
public function index3(){
$array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
$array2 = [
"id" => [1, 2, 3],
"zone_id" => [4, 5, 6],
"country_id" => [7, 8, 9],
"3" =>[10, 11, 12, 13]
];
dump($array2);
$this->assign('arr1', $array1);
$this->assign('arr2', $array2);
return view();
}
<span>foreach循环输出一维数组</span>
{foreach name="arr1" item="vo"}
<span>{$vo}</span>
{/foreach}
<hr/>
<span>volist循环输出一维数组</span>
{volist name="arr1" item="vo"}
<span>{$vo}</span>
{/volist}
<hr/>
1.2. volist(错误) 与 foreach(正确) 输出二维数组
public function index3(){
$array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
$array2 = [
"id" => [1, 2, 3],
"zone_id" => [4, 5, 6],
"country_id" => [7, 8, 9],
"3" =>[10, 11, 12, 13]
];
dump($array2);
$this->assign('arr1', $array1);
$this->assign('arr2', $array2);
return view();
}
<!-- 遍历外层数组 -->
{foreach name="arr2" item="wo" key="key"}
<!-- 判断是否为关联数组的键 -->
<!-- 遍历内层数组 -->
{foreach name="wo" item="item"}
<span>{$item}</span>
{/foreach}
{/foreach}
<hr/>
{volist name="arr2" item="wo" key="key"}
<!-- 判断是否为关联数组的键 -->
<!-- 遍历内层数组 -->
{volist name="wo" item="item"}
<span>{$item}</span>
{/volist}
{/volist}
<hr/>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>筛选币商白名单</title>
</head>
<body>
<h1>筛选币商白名单</h1>
<form action="{url:('/admin/select/index)'}" method="post">
<div>
{:lang('foreach国家选择')}:
<select class="form-control" name="country1"(上传的键) style="width: 140px;">
<option value="-1">--</option>
{foreach name="data" item="vo"}
<option value="{$vo.country_code(上传的值)}">{$vo.name(显示的值)}</option>
{/foreach}
</select>
<div>
<button type="submit">提交</button>
</div>
</div>
</form>
<form action="{url:('/admin/select/index')}" method="post">
<div>
{:lang('volist国家选择')}:
<select class="form-control" name="country2" style="width: 140px;">
<option value="-1">--</option>
{volist name="data" id="wo"}
<option value ="{$wo.country_code}">{$wo.name}</option>
{/volist}
</select>
</div>
<div>
<button type="submit">提交</button>
</div>
</form>
</form>
</body>
</html>
1.3. volist(错误) 与 foreach(正确) 输出特定的键
public function index3(){
$array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
$array2 = [
"id" => [1, 2, 3],
"zone_id" => [4, 5, 6],
"country_id" => [7, 8, 9],
"3" =>[10, 11, 12, 13]
];
$this->assign('arr1', $array1);
$this->assign('arr2', $array2);
return view();
}
<!-- 遍历外层数组 -->
{foreach name="arr2" item="wo" key="item"}
<!-- 判断当前键是否为 "zone_id" -->
{if $item == 'zone_id'}
<!-- 遍历 "zone_id" 对应的内层数组 -->
{foreach name="wo" item="data"}
<span>{$data}</span>
{/foreach}
{/if}
{/foreach}
<hr/>
<!-- 遍历外层数组 -->
{volist name="arr2" item="wo" key="item"}
<!-- 判断当前键是否为 "zone_id" -->
{if $item == 'zone_id'}
<!-- 遍历 "zone_id" 对应的内层数组 -->
{volist name="wo" item="data"}
<span>{$data}</span>
{/volist}
{/if}
{/volist}
<hr/>
1.4. volist 输出 用来输出数据库的查询
public function index3(){
$array1 = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21];
$array2 = [
"id" => [1, 2, 3],
"zone_id" => [4, 5, 6],
"country_id" => [7, 8, 9],
"3" =>[10, 11, 12, 13]
];
$data = Db::table('country')
->alias('c')
->join(['user'=>'u'],'c.country_code = u.country_code','right')
->field('c.*,u.zone_id')
->select();
dump($data);
$this->assign('arr1', $array1);
$this->assign('arr2', $array2);
$this->assign('arr3', $data);
return view();
}
{volist name="arr3" id="vo"}
<span>{$vo.zone_id}</span>
{/volist}
1.5. foreach($data as $k => $dd) ,$k为数组的下标,$dd为数组里面的内容
public function index2(){
$data = [
['id' => 1 ,'name' => 'ljl','age'=>22,'gender'=>"男"],
['id' => 2,'name' => 'xbl','age'=>20,'gender'=>"男"]
];
foreach($data as $k => $dd) {
dump("¥k的值是".$k);
$t1str = "我是";
$t1str .= $dd['name'].","; // 所有元素的id追加到$t1str
if($k % 2 == 0) {
$t1str .= $dd['name']."你好,你为偶数"; // 偶数索引的id追加到$t1str1
dump($t1str);
} else {
$t1str .= $dd['name']."你好,你为奇数"; // 奇数索引的id追加到$t1str2
dump($t1str);
}
}
}