I've defined a pair of JSON data key-value pairs, K for key V to represent values
Values with \ r \ n (line break)
That is, I want to merge two PHP arrays, the same as K and value, ignoring one, if K is the same, V is different, the V is merged, the value is merged, the same slpit get the array, and then take an intersection
[
{' K ': ' k111111 ', ' V ': ' v11111\r\nv11112\r\nv11113 '},
{' K ': ' k22222 ', ' V ': ' v22222 '},
{' K ': ' k33333, ' V ': ' v33333 '},
{' K ': ' k44444 ', ' V ': ' v44443\r\nv44444 '}
]
PHP Json_decode function to get the array
A second JSON
[
{' K ': ' k111111 ', ' V ': ' v11111 '},
{' K ': ' k33333, ' V ': ' v33333 '},
{' K ': ' k44444 ', ' V ': ' v44444\r\nv44445\r\nv44446 '},
{' K ': ' k55555 ', ' V ': ' v55555 '}
]
The results obtained:
[
{' K ': ' k111111 ', ' V ': ' v11111\r\nv11112\r\nv11113 '},
{' K ': ' k22222 ', ' V ': ' v22222 '},
{' K ': ' k33333, ' V ': ' v33333 '},
{' K ': ' k44444 ', ' V ': ' v44443\r\nv44444\r\nv44445\r\nv44446 '},
{' K ': ' k55555 ', ' V ': ' v55555 '}
]
That means K.
May I ask you, the gods, how to achieve
Reply to discussion (solution)
Wrong not to take the intersection, take the number of the more
No one's coming, big gods.
$a =<<< ' JSON ' [{' K ': ' k111111 ', ' V ': ' v11111\r\nv11112\r\nv11113 '},{' K ': ' k22222 ', ' V ': ' v22222 '},{' k ': ' k33333 ', ' V ': ' v33333 '},{' k ': ' k44444 ', ' V ': ' v44443\r\nv44444 '}]json; $a = str_replace ("' ", '" ', $a); $a = Json_decode ($a, 1); $b =<<< ' json ' [{' K ': ' k111111 ', ' V ': ' v11111 '},{' k ': ' k33333 ', ' V ': ' v33333 '}, {' K ': ' k44444 ', ' V ': ' v44444\r\nv44445\r\nv44446 '},{' K ': ' k55555 ', ' V ': ' v55555 '}]json; $b = Str_replace ("'", ' "', $b); $b = Json_decode ($b, 1), $res = Array (), foreach ($a as $r) $res [$r [' k ']] = $r, foreach ($b as $r) {$k = $r [' K ']; if (! isset ($res [$k])) {$res [$k] = $r; Continue if ($res [$k] [' V '] = = $r [' V ']) continue; $res [$k] [' V '] = join ("\ r \ n", Array_unique (Array_merge ("\ r \ n", $res [$k] [' V ']), explode ("\ r \ n", $r [' V '])));} Echo Json_encode (Array_values ($res));
[{"K": "k111111", "V": "v11111\r\nv11112\r\nv11113"},{"K": "k22222", "V": "v22222"},{"K": "k33333", "V": "v33333"},{"K" : "k44444", "V": "v44443\r\nv44444\r\nv44445\r\nv44446"},{"K": "k55555", "V": "v55555"}]