当前位置:网站首页>PHP merges multiple arrays. The same element takes the intersection of different elements to form an array
PHP merges multiple arrays. The same element takes the intersection of different elements to form an array
2022-07-01 09:24:00 【Does fried dough twist hurt】
php Merge multiple arrays Take the intersection of the same elements Different elements form an array
Brother Zha Published on 2014/01/23 22:00 read 3K+ Collection 4 answer 10
PHP
For help :
The array format is like this :
$arr=array(
array(‘element1’=>‘a’,‘element2’=>1),
array(‘element1’=>‘b’,‘element2’=>2),
array(‘element1’=>‘a’,‘element2’=>2),
array(‘element1’=>‘b’,‘element2’=>3),
);
Need to synthesize as :
$arr=array(
array(‘element1’=>‘a’,‘element2’=>array(1,2)),
array(‘element1’=>‘b’,‘element2’=>array(2,3)),
);
report
0
Brother Zha
Brother Zha 2014/01/23 22:50
n e w = a r r a y ( ) ; f o r e a c h ( new = array(); foreach ( new=array();foreach(arr as $key => $value) {
n e w [ new[ new[value[‘element1’]][] = $value[‘element2’];
}
n e w 2 = a r r a y ( ) ; f o r e a c h ( new2 = array(); foreach( new2=array();foreach(new as k = > k=> k=>v){
n e w 2 [ ] = a r r a y ( ′ e l e m e n t 1 ′ = > new2[] = array('element1'=> new2[]=array(′element1′=>k,‘element2’=>KaTeX parse error: Expected 'EOF', got '}' at position 5: v); }̲ print_r(new2);
Comment on (0) Quote this answer report
0
Wine free
Wine free 2014/01/24 08:39
Ask and answer ?
Comment on (0) Quote this answer report
0
Practicing typing
Practicing typing 2014/01/24 09:04
You are so efficient … One cycle is enough , direct k operation , Value merge array , Finished …
Comment on (0) Quote this answer report
0
Brother Zha
Brother Zha 2014/01/24 09:44
Reference from “cgf986916” The answer
You are so efficient … One cycle is enough , direct k operation , Value merge array , Finished …
For more efficient writing
Comment on (0) Quote this answer report
0
tretretre0000000000
tretretre0000000000 2014/01/24 10:18
$new_array = array();
a = a r r a y ( ) ; f o r e a c h ( a = array(); foreach( a=array();foreach(arr as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ if(!in_array(val[‘element1’],$a)){
$a[] = $val[‘element1’];
n e w a r r a y [ new_array[ newarray[val[‘element1’]] = $val;
}else{
n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’] = array_merge(array( n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’]),array($val[‘element2’]));
}
}
n e w a r r a y = a r r a y v a l u e s ( new_array = array_values( newarray=arrayvalues(new_array);
In fact, one cycle can really . dedicated , try .
Comment on (1) Quote this answer report
Practicing typing
Practicing typing 2014/01/24 11:36
You have a good one , But it can improve
reply report
0
List of Posts
List of Posts 2014/01/24 10:20
$arr=array(
array(‘element1’=>‘a’,‘element2’=>1),
array(‘element1’=>‘b’,‘element2’=>2),
array(‘element1’=>‘a’,‘element2’=>2),
array(‘element1’=>‘b’,‘element2’=>3),
);
class timer
{
private $start_time = NULL;
private $end_time = NULL;
private function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function start()
{
$this->start_time = $this->getmicrotime();
}
function stop()
{
$this->end_time = $this->getmicrotime();
}
function result()
{
if (is_null($this->start_time))
{
exit('Timer: start method not called !');
return false;
}
else if (is_null($this->end_time))
{
exit('Timer: stop method not called !');
return false;
}
return round(($this->end_time - $this->start_time), 4);
}
# an alias of result function
function time()
{
$this->result();
}
}
// @pxv Methods 0.0343 second
$timer = new timer();
$timer->start();
i = 0 ; f o r ( i = 0; for( i=0;for(i = 0; $i<10000; $i++){
$new_array = array();
a = a r r a y ( ) ; f o r e a c h ( a = array(); foreach( a=array();foreach(arr as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ if(!in_array(val[‘element1’],$a)){
$a[] = $val[‘element1’];
n e w a r r a y [ new_array[ newarray[val[‘element1’]] = $val;
}else{
n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’] = array_merge(array( n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’]),array($val[‘element2’]));
}
}
n e w a r r a y = a r r a y v a l u e s ( new_array = array_values( newarray=arrayvalues(new_array);
}
$timer->stop();
echo $timer->result();
echo ‘|’;
// My method 0.0522 second
$timer = new timer();
$timer->start();
i = 0 ; f o r ( i = 0; for( i=0;for(i = 0; $i<10000; $i++){
a r r C h e c k = a r r a y u n i q u e ( a r r a y m a p ( f u n c t i o n ( arrCheck = array_unique(array_map(function( arrCheck=arrayunique(arraymap(function(subArr){return $subArr[‘element1’];}, $arr));
a r r R e s u l t = a r r a y ( ) ; f o r e a c h ( arrResult = array(); foreach( arrResult=array();foreach(arrCheck as $name){
t m p = a r r a y ( ) ; f o r e a c h ( tmp = array(); foreach( tmp=array();foreach(arr as KaTeX parse error: Expected '}', got 'EOF' at end of input: …r){ if(subArr[‘element1’] == $name){
$tmp[] = KaTeX parse error: Expected 'EOF', got '}' at position 29: …nt2']; }̲ } arra…arrResult, array(‘element1’ => $name, ‘element2’ => KaTeX parse error: Expected 'EOF', got '}' at position 8: tmp)); }̲ //var_dump(arrResult);
}
$timer->stop();
echo $timer->result();
echo ‘|’;
// Brother monkey's method 0.0164 second
$timer = new timer();
$timer->start();
i = 0 ; f o r ( i = 0; for( i=0;for(i = 0; $i<10000; $i++){
n e w = a r r a y ( ) ; f o r e a c h ( new = array(); foreach ( new=array();foreach(arr as $key => $value) {
n e w [ new[ new[value[‘element1’]][] = $value[‘element2’];
}
n e w 2 = a r r a y ( ) ; f o r e a c h ( new2 = array(); foreach( new2=array();foreach(new as k = > k=> k=>v){
n e w 2 [ ] = a r r a y ( ′ e l e m e n t 1 ′ = > new2[] = array('element1'=> new2[]=array(′element1′=>k,‘element2’=>KaTeX parse error: Expected 'EOF', got '}' at position 5: v); }̲ //print_r(new2);
}
$timer->stop();
echo $timer->result();
Practicing typing 2014/01/24 11:38
Reference from “pxv” The answer
$new_array = array();
a = a r r a y ( ) ; f o r e a c h ( a = array(); foreach( a=array();foreach(arr as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ if(!in_array(val[‘element1’],$a)){
$a[] = $val[‘element1’];
n e w a r r a y [ new_array[ newarray[val[‘element1’]] = $val;
}else{
n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’] = array_merge(array( n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’]),array($val[‘element2’]));
}
}
n e w a r r a y = a r r a y v a l u e s ( new_array = array_values( newarray=arrayvalues(new_array);
In fact, one cycle can really . dedicated , try .
n e w a r r a y = a r r a y ( ) ; f o r e a c h ( new_array = array(); foreach ( newarray=array();foreach(arr as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: … isset(new_array[$val[‘element1’]])
? n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’] = array_merge(array( n e w a r r a y [ new_array[ newarray[val[‘element1’]][‘element2’]), array($val[‘element2’]))
: n e w a r r a y [ new_array[ newarray[val[‘element1’]] = $val;
}
n e w a r r a y = a r r a y v a l u e s ( new_array = array_values( newarray=arrayvalues(new_array);
https://www.oschina.net/question/244077_142022
边栏推荐
- 2.2 【pytorch】torchvision. transforms
- MapReduce编程基础
- JS prototype trap
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- JS rewrite their own functions
- FAQ | FAQ for building applications for large screen devices
- js作用域链与闭包
- nacos简易实现负载均衡
- Flink interview questions
- 美团2022年机试
猜你喜欢
dsPIC30F6014a LCD 方块显示
【pytorch】2.4 卷积函数 nn.conv2d
小鸟识别APP
钓鱼识别app
Jetson nano installs tensorflow GPU and problem solving
An overview of the design of royalties and service fees of mainstream NFT market platforms
Network counting 01 physical layer
Pain points and solutions of equipment management in large factories
How to manage fixed assets efficiently in one stop?
NiO zero copy
随机推荐
An overview of the design of royalties and service fees of mainstream NFT market platforms
序列化、监听、自定义注解
3D打印Arduino 四轴飞行器
Summary of reptile knowledge points
Understand shallow replication and deep replication through code examples
JS functionarguments object
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
Understanding and implementation of AVL tree
手指点击屏幕就模拟进入F11进入全屏
phpexcel 里 获取某一列的列表 获取某一列的字母
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
js 使用toString 区分Object、Array
JS prototype inheritance can only inherit instances, not constructors
pcl_ Viewer command
Microcomputer principle - bus and its formation
es6-顶层对象与window的脱钩
Log4j 日志框架
Promise asynchronous programming
SDN_简单总结
nacos简易实现负载均衡