代码如下:$data 是一个数组。
if(is_array($data) && !empty($data )){ $elasticsearch = Yii::$app->elasticsearch; $bulkclient = $elasticsearch->createBulkCommand(); # elasticSearch的index,相当于mysql的数据库 $index_name = 'fecshop'; # elasticSearch的type,相当于mysql的table $type_name = 'whole_free_sku_data'; echo $index_name."###".$type_name." \n"; foreach($data as $one){ $i++; $a = []; $a['sku_id'] = $one['_id']; $value = $one['value']; # 将多维数组部分进行序列化。 if(is_array($value) && !empty($value )){ foreach($value as $k => $v){ if(in_array($k,['devide','country_code','browser_name','operate'])){ if(is_array($v) && !empty($v)){ $vv = []; foreach($v as $vk => $o){ $vv[] = serialize([$vk=>$o]); } $v = $vv; } } $a[$k] = $v; } } # 去掉_id字段。 unset($a['_id']); $bulkclient->addAction(array( 'index' => array( '_index'=> $index_name, '_type' => $type_name, '_id' => $one['_id'], ) ), $a); } $bulkclient->execute(); }