简体   繁体   English

如何在 url 中获取参数,它是数组? yii2

[英]How to get params in url , and it is array? yii2

I have url like this我有这样的网址

http://localhost/belajar4/web/index.php?r=data%2Fsingle&id=2&DataSearch[TANGGAL]=2015-08-04&DataSearch[TANGGAL_SELESAI]=2015-08-12

And I want to get DataSearch['Tanggal'] dan DataSearch[TANGGAL_SELESAI]我想得到 DataSearch['Tanggal'] dan DataSearch[TANGGAL_SELESAI]

I have tried for id with我试过用

Yii::$app->request->queryParams['id']

And it was success for it, but not with DataSearch['Tanggal']它是成功的,但不是 DataSearch['Tanggal']

When I try for DataSearch['Tanggal'] the error is当我尝试 DataSearch['Tanggal'] 时,错误是

Undefined index: DataSearch[TANGGAL]

I think it should have easy to answer but i am newbie for yii2 and i didnt find the solution yet我认为应该很容易回答,但我是 yii2 的新手,我还没有找到解决方案

You can get them from array like this:您可以像这样从数组中获取它们:

Yii::$app->request->queryParams['DataSearch']["TANGGAL_SELESAI"]
Yii::$app->request->queryParams['DataSearch']["TANGGAL"]

You can also use combination of ArrayHelper::getValue() and Yii::$app->request->get() with dot notation:您还可以使用ArrayHelper::getValue()Yii::$app->request->get()

use Yii;
use yii\helpers\ArrayHelper;


...

$value = ArrayHelper::getValue(Yii::$app->request->get(), 'DataSearch.TANGGAL_SELESAI');

The main advandage is you can avoid Undefined index exception and change default value (third parameter).主要优点是您可以避免Undefined index异常并更改默认值(第三个参数)。

Official docs:官方文档:

I had faced slimier issue & spent lots of time to get the value but somehow I found solution like below.我遇到了更棘手的问题并花了很多时间来获得价值,但不知何故我找到了如下解决方案。

// Trying to print the value // 尝试打印值

echo Yii::$app->request->queryParams['DataSearch']['"TANGGAL_SELESAI"'];

echo Yii::$app->request->queryParams['DataSearch']['"TANGGAL"'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM