当前位置:网站首页>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
边栏推荐
- Redis source code learning (29), compressed list learning, ziplist C (II)
- Databinding source code analysis
- How to solve the problem of fixed assets management and inventory?
- 2.2 【pytorch】torchvision.transforms
- Problems caused by delete and delete[]
- MT7628K eCos开发入门
- phpexcel 里 获取某一列的列表 获取某一列的字母
- [video game training] real topic of 2013 video game of infrared optical communication device
- JS scope chain and closure
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
猜你喜欢

Pain points and solutions of fixed assets management of group companies

树结构---二叉树2非递归遍历

Mysql 优化

计网01-物理层

Jetson nano installs tensorflow GPU and problem solving

Implementation and application of queue

dsPIC30F6014a LCD 方块显示

Which method is good for the management of fixed assets of small and medium-sized enterprises?

I use flask to write the website "one"

队列的实现和应用
随机推荐
Understand shallow replication and deep replication through code examples
PR training notes
NiO zero copy
Latex插入的eps图片模糊解决方法
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
Click the screen with your finger to simulate F11 and enter the full screen
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
【pytorch】nn.AdaptiveMaxPool2d
MySQL optimization
Error org apache. catalina. core. StandardContext. FilterStart start filter exception
JS rewrite their own functions
Nacos service configuration and persistence configuration
Learning practice: comprehensive application of cycle and branch structure (II)
MapReduce编程基础
Leetcode daily question brushing record --540 A single element in an ordered array
Which method is good for the management of fixed assets of small and medium-sized enterprises?
小鸟识别APP
Set the type of the input tag to number, and remove the up and down arrows