我试图在一个结果中合并两个数组
$array1 =["pack_id","pack_name","cash"];
$array2 =["pack_id","pack_name","inactifs","actifs","total"];
我的下面的代码:
for ($i = 0; $i < count($cashByPacks); $i++) {
$result[$i] = array('pack_id' => $cashByPacks[$i]->pack_id, 'pack_name' => $cashByPacks[$i]->pack_name, 'effectif' => 0, 'cash' => $cashByPacks[$i]->cash ?? 0, 'actif' => 0, 'inactif' => 0, 'percent' => 0);
for ($j = 0; $j < count($query); $j++) {
if (array_search($query[$j]->pack_id, $result[$i])) {
$result[$j] = array('pack_id' => $query[$j]->pack_id, 'pack_name' => $query[$j]->pack_name, 'effectif' => $query[$j]->total, 'actif' => $query[$j]->actifs, 'inactif' => $query[$j]->inactifs, 'cash' => $cashByPacks[$i]->cash ?? 0, 'percent' => ($cashByPacks[$i]->cash == 0) ? 0 : round(($cashByPacks[$i]->cash / $totalcash) * 100, 2));
}
}
}
return $result;
我希望输出将是一个包含以下内容的数组:
["pack_id","pack_name","cash","actifs","inactifs","totale"]
答案 0 :(得分:0)
使用array_merge和array_diff。
Array_diff查找两个数组之间的区别,然后array_merge将两个数组合并。
import socket
UDP_IP = '8.8.8.8'
UDP_PORT = 53
dn = 'www.google.com'
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
msg = '???'
sock.sendto(msg, (UDP_IP, UDP_PORT))
(data, addr) = sock.recvfrom(1024)
print data, addr
或使用array_merge和array_unique。
Array_merge将合并两个数组并创建重复项,array_unique将删除重复项。
$arr1 = ["pack_id","pack_name","cash"];
$arr2 =["pack_id","pack_name","inactifs","actifs","total"];
$res = array_merge($arr1, array_diff($arr2, $arr1));
var_dump($res);