当前位置:网站首页>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
边栏推荐
- 钓鱼识别app
- Exception handling of classes in C #
- How to effectively align team cognition
- Niuke monthly race 22- collect pieces of paper
- 美团2022年机试
- JS functionarguments object
- Tree structure -- binary tree 2 non recursive traversal
- Solution of EPS image blur by latex insertion
- Pain points and solutions of fixed assets management of group companies
- How to launch circle of friends marketing and wechat group activities
猜你喜欢

An overview of the design of royalties and service fees of mainstream NFT market platforms
![[pytorch] softmax function](/img/97/b8ae22e8496a77e665d716cb0e9ee3.png)
[pytorch] softmax function

2.2 【pytorch】torchvision.transforms

Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)

js作用域链与闭包

How to solve the problem of fixed assets management and inventory?

OSPF - virtual link details (including configuration commands)

小鸟识别APP
![2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder](/img/19/cce8d8a7cdcb1021166c46adf803c1.png)
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder

Why is the Ltd independent station a Web3.0 website!
随机推荐
钓鱼识别app
Rich text interpolation
Error org apache. catalina. core. StandardContext. FilterStart start filter exception
2.4 activation function
Differences between JS valueof and toString
How to effectively align team cognition
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
Analysis and solution of JS this loss
3D printing Arduino four axis aircraft
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Log4j log framework
I use flask to write the website "one"
js this丢失问题分析 及 解决方案
闭包实现迭代器效果
Imitation of Baidu search results top navigation bar effect
Latex插入的eps图片模糊解决方法
Leetcode daily question brushing record --540 A single element in an ordered array
Can diffusion models be regarded as an autoencoder?
[video game training] real topic of 2013 video game of infrared optical communication device
Summary of reptile knowledge points