我想要唯一地合并json数据。我有两个json数据,需要一个生成的json数据,它以独特的方式包含两个json数据。
$json1 = '[{"name":"A1","phone":"123"}, {"name":"A2","phone":"456"}]';
$json2 = '[{"name":"A1","phone":"123"}, {"name":"A3","phone":"789"}]';
$decoded_json1 = json_decode($json1, true);
$decoded_json2 = json_decode($json2, true);
var_dump($decoded_json1);
var_dump($decoded_json2);
$merge_json = array_merge($decoded_json1, $decoded_json2);
var_dump($merge_json);
/* I want json data like this
[{"name":"A1","phone":"123"}, {"name":"A2","phone":"456"}, {"name":"A3","phone":"789"}]
*/
但上面的代码不能正常合并json数据,但问题是它不是唯一的这个对象{"name":"A1","phone":"123"}
重复。
更新
$json2 = '[{"name":"A1","phone":"123"}, {"name":"A3","phone":"789"}, {"name":"A1","phone":"000"}]';
答案 0 :(得分:0)
PHP并不知道什么是独特的,什么不是,你需要手动执行此操作。
码
<?php
$json1 = '[{"name":"A1","phone":"123"}, {"name":"A2","phone":"456"}]';
$json2 = '[{"name":"A1","phone":"123"}, {"name":"A3","phone":"789"}]';
$decoded_json1 = json_decode($json1, true);
$decoded_json2 = json_decode($json2, true);
$newArray = [];
foreach($decoded_json1 as $row) {
$hash = hash('SHA256', json_encode($row));
$newArray[$hash] = $row;
}
foreach($decoded_json2 as $row) {
$hash = hash('SHA256', json_encode($row));
$newArray[$hash] = $row;
}
// remove the hash keys
$newArray = array_values($newArray);
var_dump($newArray);
输出
array(3) {
[0]=>
array(2) {
["name"]=>
string(2) "A1"
["phone"]=>
string(3) "123"
}
[1]=>
array(2) {
["name"]=>
string(2) "A2"
["phone"]=>
string(3) "456"
}
[2]=>
array(2) {
["name"]=>
string(2) "A3"
["phone"]=>
string(3) "789"
}
}
答案 1 :(得分:0)
array_merge()仅过滤重复的关键成员,因此您可以使用json_encode()
,json_decode()
和array_unique()
来执行此操作:
$tmp = array_map('json_encode', $merge_json); // Encode everyone array members.
$tmp = array_unique($tmp); // Filter duplicated
$tmp = array_values($tmp); // Re generate array indexes
$tmp = json_encode($tmp);
echo $tmp;
结果:
[{"name":"A1","phone":"123"}","{"name":"A2","phone":"456"}","{"name":"A3","phone":"789"}]
答案 2 :(得分:0)
试试这个:
<强>更新强>
func setupView() {
session = AVCaptureSession()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.resume))
addGestureRecognizer(tap)
// Set the captureDevice.
let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
// Create input object.
let videoInput: AVCaptureDeviceInput?
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
} catch {
return
}
// Add input to the session.
if (session!.canAddInput(videoInput)) {
session!.addInput(videoInput)
} else {
scanningNotPossible()
}
// Create output object.
let metadataOutput = AVCaptureMetadataOutput()
// Add output to the session.
if (session!.canAddOutput(metadataOutput)) {
session!.addOutput(metadataOutput)
// Send captured data to the delegate object via a serial queue.
metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
// Set barcode type for which to scan: EAN-13.
metadataOutput.metadataObjectTypes = [
AVMetadataObjectTypeCode128Code
]
} else {
scanningNotPossible()
}
// Determine the size of the region of interest
let x = self.frame.origin.x/UIScreen.main.bounds.width
let y = self.frame.origin.y/UIScreen.main.bounds.height
let width = self.frame.width/UIScreen.main.bounds.height
let height = self.frame.height/UIScreen.main.bounds.height
let scanRectTransformed = CGRect(x: x, y: y, width: 1, height: height)
metadataOutput.metadataOutputRectOfInterest(for: scanRectTransformed)
// Add previewLayer and have it show the video data.
previewLayer = AVCaptureVideoPreviewLayer(session: session)
previewLayer.frame = self.bounds
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
layer.addSublayer(previewLayer)
// Begin the capture session.
session!.startRunning()
}
Json结果:
$json1 = '[{"name":"A1","phone":"123"}, {"name":"A2","phone":"456"}]';
$json2 = '[{"name":"A1","phone":"123"}, {"name":"A3","phone":"789"}, {"name":"A1","phone":"000"}]';
$array = array_unique(array_merge(json_decode($json1, true), json_decode($json2, true)), SORT_REGULAR);
function data_uniquely($key, $array) {
$_array = array();
foreach ($array as $v) {
if (isset($_array[$v[$key]]))
continue;
$_array[$v[$key]] = $v;
}
$array = array_values($_array);
return json_encode($array);
}
$json = data_uniquely('name', $array);
echo $json;