PHP code example of yii2mod / yii2-array-query
1. Go to this page and download the library: Download yii2mod/yii2-array-query library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2mod / yii2-array-query example snippets
$data = [
[
'id' => 1,
'username' => 'admin',
'email' => '[email protected] '
],
[
'id' => 2,
'username' => 'test',
'email' => '[email protected] '
],
];
$query = new ArrayQuery();
$query->from($data);
$query->where(['username' => 'admin']);
$rows = $query->all();
// Some search model
/**
* Setup search function for filtering and sorting.
* @param $params
* @return ArrayDataProvider
*/
public function search($params)
{
$models = User::find()->asArray()->all();
$query = new ArrayQuery();
$query->from($models);
// load the search form data and validate
if ($this->load($params) && $this->validate()) {
// adjust the query by adding the filters
$query->andFilterWhere(['id' => $this->id]);
$query->andFilterWhere(['status' => $this->status]);
$query->andFilterWhere(['like', 'username', $this->username]);
$query->andFilterWhere(['like', 'email', $this->email]);
}
// prepare the ArrayDataProvider
return new ArrayDataProvider([
'allModels' => $query->indexBy('id')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email', 'status'],
],
'pagination' => [
'pageSize' => 10
],
]);
}
php composer.phar
"yii2mod/yii2-array-query": "*"