Recently, because of the project, I want to use voist. In this process, there are some small problems, mainly how to output key when voist outputs multiple data circularly. I read a lot of materials on the Internet, but I'm disappointed that most of them are the instructions for pasting and copying the voist label on the Thinkphp manual. In order to help others, I decided to write this article. (in this case, the framework used is thinkephp3.2, and other related knowledge is php array). First paste out the code in the controller:
public function check() { $multiArr = [['a' => ['num' => '1',], 'b' => ['num' => '2',]], ['c' => ['num' => '3',], 'd' => ['num' => '4',]]]; $this->assign('list', $multiArr); $this->display(); }
The printed format is as follows:
Array ( [0] => Array ( [a] => Array ( [num] => 1 ) [b] => Array ( [num] => 2 ) ) [1] => Array ( [c] => Array ( [num] => 3 ) [d] => Array ( [num] => 4 ) ) )
Obviously, this is a multiple array. If you want to get their key value, if you use voist, the code is as follows:
<tr> <volist name="list" id="data" key="k"> <td>{$k}</td> <td> <tr> <volist name="data" id="vo"> <td>{$key}</td> <td>{$vo.num}</td> </volist> </tr> </td> </volist> </tr>
The results are as follows:
1 a 1 b 2 2 c 3 d 4
In this case, it is obvious that the key value of the external loop is the system default, while the key value of the internal loop is determined by the data itself rather than by the loop.
In the Thinkphp manual, this is very clear. If there are no other requirements here, it can meet most of the requirements of outputting key values. But if the array is changed to the following format:
$multiArr = [ 'aaa' => [ 'a' => [ 'num' => '1', ], 'b' => [ 'num' => '2', ] ], 'bbb' => [ 'c' => [ 'num' => '3', ], 'd' => [ 'num' => '4', ] ] ];
And at this time, the key value of the outer loop and the key value of the inner loop are determined by the data itself, rather than controlled by the loop. What should we do? Well, I got into a mistake at that time. I always wanted to use two voists to loop, and then output the key value (which is determined by the data itself), but I tried for a long time and found that it still couldn't work. Looking up a lot of information, I found that many didn't mention the problem. Murdering countless brain cells, looking at the code, I suddenly thought that foreach can also do this kind of circular operation, why not try to use this label? Or a combination of these two labels? All of a sudden, it was clear. Therefore, change the front-end code to the following:
<tr> <foreach name="list" item="data" key="i"> <td>{$i}</td> <td> <tr> <volist name="data" id="vo" key="k"> <td>{$key}</td> <td>{$vo.num}</td> </volist> </tr> </td> </foreach> </tr>
The results are as follows:
aaa a 1 b 2 bbb c 3 d 4
The above requirements are met. The summary is as follows:
In thinkphp, if you want to output the key value controlled by the data instead of the default value when using the voist tag for multiple array circular output, you can use the foreach tag and the voist tag together to output the key value as it is.
Article from: https://www.bbsmax.com/A/A2dmgekBJe/