PHP合并JSON数组 [英] php merge json arrays

查看:112
本文介绍了PHP合并JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数组1:

[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]

数组2:

[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]

这两者都是使用fetch_assoc_all()从mySQL数据库中提取的

Those both were pulled from mySQL database using fetch_assoc_all()

我尝试了merge_array,merge_array_recursive,json_decode(xx,true)以及我可以想到的各种事情,以及通过Google进行的其他工作.我正在寻找一种将array1,array2合并为类似方式的方法:

I tried merge_array, merge_array_recursive, json_decode(xx,true) and all kinds of things I could think on top of my head and elsewhere via google. I'm looking for a way to merge both array1, array2 into something like:

[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000","Trouble":"2"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0","Trouble":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0","Trouble":"0"}
]

PlayerID始终是唯一的.希望听到我能做些什么来合并这两个数组(array1,array2)

PlayerID are always unique. Hope to hear what insight I could do to merge those 2 arrays (array1,array2)

(附加/编辑) 对于那些想知道mySQL是什么样子的人(我无法在JOIN语句上大打折扣):

(Additonal/Edit) For those wondering what's mySQL looks like (I couldn't wrap my head around on JOIN statement):

$mSQL = 'SELECT nPlayer.PlayerID,userName,castleCount,IF(LastUpdate < (UNIX_TIMESTAMP() - 3720),LastUpdate*1000,0) NotUpd';
$mSQL .= ' FROM nPlayer';
$mSQL .= ' LEFT JOIN nMains ON nMains.mID = nPlayer.mID';
$mSQL .= ' WHERE nMains.Main = "'.$M.'"  AND nMains.Pass = "'.md5($P).'" AND nMains.Server = "'.$S.'"';
$mSQL .= ' ORDER BY nPlayer.PlayerID';

$mSQL = 'SELECT nCity.PlayerID,SUM(IF(Wartown > 0,1,0))+SUM(IF(support < 100,1,0))) Trouble';
$mSQL .= ' FROM nCity';
$mSQL .= ' INNER JOIN nPlayer ON nPlayer.PlayerID = nCity.PlayerID AND nPlayer.mID = nCity.mID';
$mSQL .= ' INNER JOIN nMains ON nMains.mID = nPlayer.mID';
$mSQL .= ' WHERE nMains.Main = "'.$M.'"  AND nMains.Pass = "'.md5($P).'" AND nMains.Server = "'.$S.'"';
$mSQL .= ' GROUP BY nCity.PlayerID';

推荐答案

详细说明

您可以根据所获得的键值来加入JSON数组,前提是您必须给出必须加入json_array()的键.

Detailed Explanation

You can join the JSON array based on the key value that you obtain provided you have to give under which key you have to join the json_array().

基于PHP代码,我将按照以下方式考虑json_objects.

I am going to consider the json_objects as follows based on the PHP code.

<?php
$array1 = '[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]';
$array2 = '[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]';
?>

因此,为了合并json_object,我们必须首先对已获得的两个数组使用json_decode().

Hence inorder to merget the json_objects we have to first use json_decode() for the both the arrays that we have obtained.

$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);

因此,json_decoded()字符串的输出将如下所示.

Hence the output for the json_decoded() string will be as follows.

第一个解码的字符串:

Array ( [0] => Array ( [PlayerID] => 17794204 [userName] => Vandiel [castleCount] => 9 [NotUpd] => 1476253231000 ) [1] => Array ( [PlayerID] => 21532584 [userName] => Mayland [castleCount] => 1 [NotUpd] => 0 ) [2] => Array ( [PlayerID] => 21539896 [userName] => Dana [castleCount] => 9 [NotUpd] => 0 ) ) 

第二个解码字符串:

Array ( [0] => Array ( [PlayerID] => 17794204 [Trouble] => 2 ) [1] => Array ( [PlayerID] => 21532584 [Trouble] => 0 ) [2] => Array ( [PlayerID] => 21539896 [Trouble] => 0 ) )

此后,我们必须根据对我们来说是uniquekey来获得merge the two arrays.

因此,该代码的功能如下.

Hence the function for the code is as follows.

我已经将 PlayerID 视为 UNIQUE 参数,并且已经组合了数组.

I have considered the PlayerID as the UNIQUE Parameter and has combined the array.

function merge_json_decoded_arrays($decode_one,$decode_two) {
    $data = array();
    $arrayAB = array_merge($decode_one,$decode_two);
    foreach ($arrayAB as $value) {
      $id = $value['PlayerID'];
      if (!isset($data[$id])) {
        $data[$id] = array();
      }
      $data[$id] = array_merge($data[$id],$value);
    }
    return $data;
  }

您需要从需要执行array_merge()操作的代码中调用这样的函数.

You need to call the function like this from the code where you need to perform the array_merge() operations.

$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);

最后,完整的代码在安装程序中显示如下.

Finally the full code appears like this with the setup.

完整代码:

<?php
$array1 = '[
{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000"},
{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0"},
{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0"}
]';
$array2 = '[
{"PlayerID":"17794204","Trouble":"2"},
{"PlayerID":"21532584","Trouble":"0"},
{"PlayerID":"21539896","Trouble":"0"}
]';

$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);

function merge_json_decoded_arrays($decode_one,$decode_two) {
    $data = array();
    $arrayAB = array_merge($decode_one,$decode_two);
    foreach ($arrayAB as $value) {
      $id = $value['PlayerID'];
      if (!isset($data[$id])) {
        $data[$id] = array();
      }
      $data[$id] = array_merge($data[$id],$value);
    }
    return $data;
  }
$merged_array = merge_json_decoded_arrays($decode_one,$decode_two);
?>

要查看合并的数组,您需要print_r()数组并查看它.

In order to view the merged array you need to print_r() the array and view it.

阵列输出代码:

print_r($merged_array);

输出:

Array ( [17794204] => Array ( [PlayerID] => 17794204 [userName] => Vandiel [castleCount] => 9 [NotUpd] => 1476253231000 [Trouble] => 2 ) [21532584] => Array ( [PlayerID] => 21532584 [userName] => Mayland [castleCount] => 1 [NotUpd] => 0 [Trouble] => 0 ) [21539896] => Array ( [PlayerID] => 21539896 [userName] => Dana [castleCount] => 9 [NotUpd] => 0 [Trouble] => 0 ) )

如果需要它作为JSON输出,则必须json_encode()获得的array()并执行操作.

If you need it as the JSON output you have to json_encode() the obtained array() and perform the operations.

注意:对于所生成的每一行,它都将unique ID作为array key.

Note: It takes the unique ID as the array key for each row that is been generated.

JSON输出代码:

print_r(json_ecode($merged_array));

输出:

{"17794204":{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000","Trouble":"2"},"21532584":{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0","Trouble":"0"},"21539896":{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0","Trouble":"0"}}

最快执行方法将采用这种方式

您需要对json_strings进行解码,然后必须通过foreach()对这两个字符串进行修饰,然后将它们与需要加入的array()组合在一起.

You need to decode the json_strings and then you have to llop both of them through the foreach() and then combine with the array() that you need to join with it.

$decode_one = json_decode($array1,TRUE);
$decode_two = json_decode($array2,TRUE);
foreach ($decode_one as $key => $first_value) {
    foreach ($decode_two as $key_two => $second_value) {
        if($first_value['PlayerID']==$second_value['PlayerID'])
        { $decode_one[$key]['Trouble'] = $second_value['Trouble'];//Here if the key exists it will join the Trouble with the First decoded array }
        else {}
    }
}
$combined_output = json_encode($decode_one); //This will return the output in json format.

输出:

[{"PlayerID":"17794204","userName":"Vandiel","castleCount":"9","NotUpd":"1476253231000","Trouble":"2"},{"PlayerID":"21532584","userName":"Mayland","castleCount":"1","NotUpd":"0","Trouble":"0"},{"PlayerID":"21539896","userName":"Dana","castleCount":"9","NotUpd":"0","Trouble":"0"}]

这篇关于PHP合并JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆