问个二位数组操作问题
$aa=array("id"=>"1","time"=>"5","neirong"=>"www");
$aa=array("id"=>"2","time"=>"55","neirong"=>"www");
$aa=array("id"=>"4","time"=>"555","neirong"=>"www");
$aa=array("id"=>"5","time"=>"555","neirong"=>"www");
$aaa=array("duibi"=>$aa);
$bb=array("id"=>"1","time"=>"5");
$bb=array("id"=>"2","time"=>"50");
$bb=array("id"=>"3","time"=>"555");
$bbb=array("duibi"=>$bb);
$aaa和$bbb对比操作
变成
$cc=array("id"=>"2","time"=>"55","neirong"=>"www");
$cc=array("id"=>"3","neirong"=>"delete");
$cc=array("id"=>"4","time"=>"555","neirong"=>"www");
$cc=array("id"=>"5","time"=>"555","neirong"=>"www");
怎么写
先说明下
$aa=array("id"=>"1","time"=>"5","neirong"=>"www");这个数据之所已没有是因为 $aa和$bb时间是一样的
$aa=array("id"=>"2","time"=>"55","neirong"=>"www");
$bb=array("id"=>"2","time"=>"50");
那是因为bbdetime比aa得time小所以。。
$cc=array("id"=>"3","neirong"=>"delete");对应的$aa不存在所以。。
$cc=array("id"=>"4","time"=>"555","neirong"=>"www");
$cc=array("id"=>"5","time"=>"555","neirong"=>"www");
因为$bb没有对应的id 4和5的数据。。所以
回复讨论(解决方案)
太基础了,回去重练...
太基础了,回去重练...
有本事你写个? 多维数组拼写起来确实费点时间,试着写了一个,不知道是不是楼主所要的结果
array (size=4)
0 =>
array (size=3)
'id' => string '3' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'delete' (length=6)
1 =>
array (size=3)
'id' => string '2' (length=1)
'time' => string '55' (length=2)
'neirong' => string 'www' (length=3)
2 =>
array (size=3)
'id' => string '4' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'www' (length=3)
3 =>
array (size=3)
'id' => string '5' (length=1)
'time' => string '555' (length=3)
'neirong' => string 'www' (length=3)
。。。。。。。。。。
$aa=array(0=>array("id"=>"1","time"=>"5","neirong"=>"www"),1=>array("id"=>"2","time"=>"55","neirong"=>"www"),2=>array("id"=>"4","time"=>"555","neirong"=>"www"),3=>array("id"=>"5","time"=>"555","neirong"=>"www"));$bb=array(0=>array("id"=>"1","time"=>"5"),1=>array("id"=>"2","time"=>"50"),2=>array("id"=>"3","time"=>"555"));$cc=array();for($i=0;$i<sizeof($bb);$i++){ $id=$bb[$i]['id']; if(!array_search($id,$aa[$i])) $cc[]=array('id'=>$id,"neirong"=>"delete");}for($i=0;$i<sizeof($aa);$i++){ for($j=0;$j<sizeof($bb);$j++){ if($aa[$i]['id']==$bb[$j]['id']){ if($aa[$i]['time']==$bb[$j]['time']) array_splice($aa,$i,1); if($aa[$i]['time']>$bb[$j]['time']){ $cc[]=$aa[$i]; array_splice($aa,$i,1); } } }}$cc=array_merge($cc,$aa);print_r($cc);
Array( [0] => Array ( [id] => 3 [neirong] => delete ) [1] => Array ( [id] => 2 [time] => 55 [neirong] => www ) [2] => Array ( [id] => 4 [time] => 555 [neirong] => www ) [3] => Array ( [id] => 5 [time] => 555 [neirong] => www ))
$aa = array( array("id"=>"1","time"=>"5","neirong"=>"www"), array("id"=>"2","time"=>"55","neirong"=>"www"), array("id"=>"4","time"=>"555","neirong"=>"www"), array("id"=>"5","time"=>"555","neirong"=>"www"),);$bb = array( array("id"=>"1","time"=>"5"), array("id"=>"2","time"=>"50"), array("id"=>"3","time"=>"555"),);foreach($aa as $v) $r[$v['id']] = $v;foreach($bb as $v) { $k = $v['id']; if(! isset($r[$k])) $r[$k] = $v; elseif($r[$k]['time'] < $v['time']) $r[$k] = $v; elseif($r[$k]['time'] == $v['time']) unset($r[$k]);}ksort($r);print_r($r);
Array( [2] => Array ( [id] => 2 [time] => 55 [neirong] => www ) [3] => Array ( [id] => 3 [time] => 555 ) [4] => Array ( [id] => 4 [time] => 555 [neirong] => www ) [5] => Array ( [id] => 5 [time] => 555 [neirong] => www ))
$aa = array( array("id"=>"1","time"=>"5","neirong"=>"www"), array("id"=>"2","time"=>"55","neirong"=>"www"), array("id"=>"4","time"=>"555","neirong"=>"www"), array("id"=>"5","time"=>"555","neirong"=>"www"),);$bb = array( array("id"=>"1","time"=>"5"), array("id"=>"2","time"=>"50"), array("id"=>"3","time"=>"555"),);foreach($aa as $v) $r[$v['id']] = $v;foreach($bb as $v) { $k = $v['id']; if(! isset($r[$k])) $r[$k] = $v; elseif($r[$k]['time'] < $v['time']) $r[$k] = $v; elseif($r[$k]['time'] == $v['time']) unset($r[$k]);}ksort($r);print_r($r);
Array( [2] => Array ( [id] => 2 [time] => 55 [neirong] => www ) [3] => Array ( [id] => 3 [time] => 555 ) [4] => Array ( [id] => 4 [time] => 555 [neirong] => www ) [5] => Array ( [id] => 5 [time] => 555 [neirong] => www ))
[3] => Array
(
[id] => 3
[time] => 555
)
不对哈 没delete
[id] => 3
[neirong] => delete
你原始的数据只有 $bb=array("id"=>"3","time"=>"555");
我不能凭空臆造出 "neirong"=>"delete"
但你可以,因为规则是你制定的
你自己加上就是了
<?php$aa[0]=array("id"=>"1","time"=>"5","neirong"=>"www");$aa[1]=array("id"=>"2","time"=>"55","neirong"=>"www");$aa[2]=array("id"=>"4","time"=>"555","neirong"=>"www");$aa[3]=array("id"=>"5","time"=>"555","neirong"=>"www");$bb[0]=array("id"=>"1","time"=>"5");$bb[1]=array("id"=>"2","time"=>"50");$bb[2]=array("id"=>"3","time"=>"555");$c = array();$d = array();$e = array();$j = array();foreach($bb as $v){ $e[] = $v['id'];}foreach($aa as $v){ $d[] = $v['id'];}$s = array_diff_assoc($e, $d);$f = array_diff_assoc($d, $e);$g = array_merge($s,$f);foreach($aa as $k=>$v){ foreach($bb as $k1=>$v1){ if($v['id']==$v1['id'] && $v1['time']>$v['time']){ $c[] = array('id'=>$v1['id'],'time'=>$v1['time'],'neirong'=>'delete'); }elseif($v['id']==$v1['id'] && $v1['time']<$v['time']){ $c[] = array('id'=>$v['id'],'time'=>$v['time'],'neirong'=>$v['neirong']); } foreach($g as $v2){ if($v['id'] == $v2 && !in_array($v['id'], $j)){ $c[] = array('id'=>$v['id'],'time'=>$v['time'],'neirong'=>$v['neirong']); $j[] = $v['id']; } if($v1['id'] == $v2 && !in_array($v1['id'], $j)){ $c[] = array('id'=>$v1['id'],'time'=>$v1['time'],'neirong'=>'delete'); $j[] = $v1['id']; } } }}var_dump($c);?>
我这个太麻烦了,自己可以改改,不太擅长简化代码
你原始的数据只有 $bb=array("id"=>"3","time"=>"555");
我不能凭空臆造出 "neirong"=>"delete"
但你可以,因为规则是你制定的
你自己加上就是了
我这个太麻烦了,自己可以改改,不太擅长简化代码
刚看版主的代码是最好的 直接遍历$aa跟$bb对比修改<?php$aa = array( array("id"=>"1","time"=>"5","neirong"=>"www"), array("id"=>"2","time"=>"55","neirong"=>"www"), array("id"=>"4","time"=>"555","neirong"=>"www"), array("id"=>"5","time"=>"555","neirong"=>"www"),); $bb = array( array("id"=>"1","time"=>"5"), array("id"=>"2","time"=>"50"), array("id"=>"3","time"=>"555"),); foreach($aa as $v) $r[$v['id']] = $v;foreach($bb as $v) { $k = $v['id']; if(! isset($r[$k])) { $v['content']='delete'; $r[$k] = $v; } elseif($r[$k]['time'] < $v['time']) $r[$k] = $v; elseif($r[$k]['time'] == $v['time']) unset($r[$k]);}ksort($r);print_r($r);?>s

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Forgetting the Win8 computer startup password is a problem that many people encounter when using computers on a daily basis. When we forget the login password, we will be unable to enter the system normally, causing inconvenience to our daily use. If you happen to encounter this problem, don’t worry. Below I will introduce some simple operations to help you quickly restore the power-on password of your Win8 computer. Method 1: Use Microsoft account password. If you use a Microsoft account to log in to your Win8 computer, you can try using the password of that account.
