首页 > 其他分享 >Yii2-助手类(ArrayHelper)

Yii2-助手类(ArrayHelper)

时间:2024-03-30 15:02:49浏览次数:24  
标签:name ArrayHelper 助手 result 数组 array Yii2 id

Yii2-助手类(ArrayHelper)

数组助手类 ArrayHelper

Yii 数组助手类提供了额外的静态方法,让你更高效的处理数组。

模型转数组

$model = Country::findOne(['code'=>'BR']);
VarDumper::dump(ArrayHelper::toArray($model));  //[ 'code' => 'BR' 'name' => 'Brazil' 'population' => 170115000 ]

获取值(getValue)

class User
{
  public $name = 'Alex';
}
$array = [
  'foo' => [
    'bar' => new User(),ddd
  ]
]

获取 name 的值

PHP 方法:

$value = isset($array['foo']['bar']->name) ? $array['foo']['name'] : null;

ArrayHelper 方法:

$value = ArrayHelper::getValue($array,'foo.bar.name');

获取列(getColumn)

从多维数组或者对象数组中获取某列的值

$data = [
  ['id' => '123','data' => 'abc'], // key 相当于对象的属性名,value 相当于对象的属性值
  ['id' => '456','data' => 'def'],
]

ArrayHelper 方法:

$ids = ArrayHelper::getColumn($data,'id');
结果:['123','456'];

移除数组指定key的项

$array = ['name'=>'xiaoming','age'=>23];
$r = ArrayHelper::remove($array,'name');
VarDumper::dump($array);

结果:['age'=>23]

移除指定值的项

$array = ['Bob' => 'Dylan', 'Michael' => 'Jackson', 'Mick' => 'Jagger', 'Janet' => 'Jackson'];
\yii\helpers\ArrayHelper::removeValue($array, 'Jackson');
VarDumper::dump($array);

结果:[ 'Bob' => 'Dylan' 'Mick' => 'Jagger' ]

根据指定的键对数组进行索引或分组

$array = [
          ['id' => '123', 'data' => 'abc', 'device' => 'laptop'],
          ['id' => '345', 'data' => 'def', 'device' => 'tablet'],
          ['id' => '345', 'data' => 'hgi', 'device' => 'smartphone'],
      ];
//索引
$result = ArrayHelper::index($array, 'id');
VarDumper::dump($result);

//分组   
$result = ArrayHelper::index($array, null,'id');

//以id和device分组
$result = ArrayHelper::index($array, 'data', [function ($element) {return $element['id'];}, 'device']);
VarDumper::dump($result);

建立映射表(map)

可以使用 map 方法从一个多维数组或者对象数组中取出数据,建立一个映射表(键值对数组), 在 map 方法的参数列表中指定了预构建的映射表的键名和值。

$array = [
['id'=>'123','name'=>'aaa','email'=>'x'],
['id'=>'456','name'=>'bbb','email'=>'y'],
['id'=>'789','name'=>'ccc','email'=>'z'],
]
// ArrayHelper 方法:
$result = ArrayHelper::map($array,'id','name');
/*
结果:
[
  '123' => 'aaa'
  '456' => 'bbb'
  '789' => 'ccc,
]
*/

转HTML 实体

$array = ['html'=>"<script>console.log('test');</script>"];
$result = ArrayHelper::htmlEncode($array);

根据指定的key筛选数组

$array = [
          'A' => [1, 2],
          'B' => [
              'C' => 1,
              'D' => 2,
          ],
          'E' => 1,
      ];
        $result = ArrayHelper::filter($array,['A','B']);

标签:name,ArrayHelper,助手,result,数组,array,Yii2,id
From: https://www.cnblogs.com/hu308830232/p/18105494

相关文章

  • Yii2-助手类(formatter)
    Yii2-助手类(formatter)示例echo\Yii::$app->formatter->asDatetime(time(),'Y-M-dH:i:s');//2023-05-1212:32:32echo\Yii::$app->formatter->asRelativeTime('1447565922');//2小时前echo\Yii::$app->for......
  • yii2-session
    yii2session开启和关闭Sessions$session=Yii::$app->session;//检查session是否开启if($session->isActive)...//开启session$session->open();//关闭session$session->close();//销毁session中所有已注册的数据$session->destroy();访问Session数据......
  • yii2-Cookie
    yii2Cookie读取Cookies$cookies=Yii::$app->request->cookies;//获取名为"language"cookie的值,如果不存在,返回默认值"en"$language=$cookies->getValue('language','en');//另一种方式获取名为"language"cookie的值i......
  • Yii2-application用法 (Yii::$app用法)
    Yii2-application用法(Yii::$app用法)配置文件config\web.phpreturn['id'=>'app-frontend','basePath'=>dirname(__DIR__),'bootstrap'=>['log'],'controllerNamespace'=>......
  • Yii2 Action用法
    Yii2Action用法actions方法publicfunctionactions(){return['error'=>['class'=>'yii\web\ErrorAction',],'captcha'=>[......
  • yii2 密码和加解密
    yii2密码和加解密生成hash$hash=Yii::$app->getSecurity()->generatePasswordHash($password);验证hashif(Yii::$app->getSecurity()->validatePassword($password,$hash)){//allgood,logginguserin}else{//wrongpassword}生成随机字符串$k......
  • yii2-分页
    yii2-分页使用ActiveDataProvider和GridViewcontrollerpublicfunctionactionIndex(){$dataProvider=newActiveDataProvider(['query'=>ArticleClass::find(),'pagination'=>['......
  • Yii2-助手类(StringHelper)
    Yii2-助手(StringHelper)截断字符串echoStringHelper::truncate('中文截断字符',4);//'中文截断...'字符串转数组StringHelper::explode('thisisstring','');//[0=>'this'1=>'is'2=>'string......
  • Yii2-助手类(Html)
    Yii2-助手类(Html)引入命名空间useyii\helpers\Html;使用<?php//【一】表单:Html::beginForm(提交地址,提交方法,属性数组);?><?=Html::beginForm('','post',['id'=>'form','class'=>'form','data'=&g......
  • yii2 小部件
    yii2小部件小部件基本上在views中使用,在视图中可调用yii\base\Widget::widget() 方法使用小部件。继承yii\base\Widget类并覆盖yii\base\Widget::init() 和/或yii\base\Widget::run() 方法可创建小部件。创建小部件<?phpnamespaceapp\widgets;useyii\base\Wid......