我想将三个数组合并在一起。所有这些数组都是完全相同的,只是内部有不同的数据,每个数组有50个项目,每个项目都有一个名为标签等的对象。
我的问题是,当我尝试将它们合并在一起然后将它们作为JSON回显时,我怎么得到不同的响应,而不是单独将它们作为JSON回显。
$tagArrays = array_merge($tags,$tags2,$tags3);
echo json_encode($tagArrays, JSON_PRETTY_PRINT);
给我不同的结果
echo json_encode($tags, JSON_PRETTY_PRINT);
echo json_encode($tags2, JSON_PRETTY_PRINT);
echo json_encode($tags3, JSON_PRETTY_PRINT);
当我说“不同的结果”时,我的意思是大不相同,似乎$ tags [i]没有被合并,而是因为他们使用相同的数据而被压扁。我希望它们合并类似于JSON_encoding它们单独工作的方式。
如果这有帮助,这就是数组的样子(缩短,想象每个数组50倍的项目对象,然后重新开始)
{
"kind": "youtube#videoListResponse",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0\/azpEKHAfo4vur0hbABsjIPqPgs4\"",
"pageInfo": {
"totalResults": 50,
"resultsPerPage": 50
},
"items": [
{
"kind": "youtube#video",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0\/s4gzHDOQRXGUOFPB3RmkIjdbdZI\"",
"id": "9LuptlmRH6k",
"snippet": {
"publishedAt": "2016-10-01T20:21:36.000Z",
"channelId": "UC1e6iW4ojbhn0gUCyvjawKg",
"title": "\u25ba STORM OF STEEL! - Battlefield 1 - 12 Minutes of Single Player Gameplay",
"description": "Battlefield 1's single player prologue, Storm of Steel! EA gave me this footage when attending a behind closed doors event at TwitchCon\n\u25ba SUBSCRIBE: http:\/\/bit.ly\/WestieSub\n\nConnect with me on other social sites:\n\n\u25ba TWITTER: http:\/\/bit.ly\/WestieTweet\n\u25ba INSTAGRAM: http:\/\/bit.ly\/InstaWestie\n\u25ba FACEBOOK: http:\/\/bit.ly\/WestieFB\n\u25ba TWITCH: http:\/\/bit.ly\/WestieTwitch\n\n\u25ba My PC Specs: http:\/\/bit.ly\/WestiePC\n\nTheWestie4321 - Westie - MrProWestie\n\n======================================\n\nWell done for reaching the bottom of the description! :D",
"thumbnails": {
"default": {
"url": "https:\/\/i.ytimg.com\/vi\/9LuptlmRH6k\/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https:\/\/i.ytimg.com\/vi\/9LuptlmRH6k\/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https:\/\/i.ytimg.com\/vi\/9LuptlmRH6k\/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https:\/\/i.ytimg.com\/vi\/9LuptlmRH6k\/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https:\/\/i.ytimg.com\/vi\/9LuptlmRH6k\/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "Westie",
"tags": [
"Battlefield 1",
"BF1",
"Battlefield 1 Multiplayer Gameplay",
"BF1 gameplay",
"Battlefield 1 gameplay",
"Battlefield 1 single player",
"bf1 single player",
"battlefield 1 war stories",
"bf1 war stories",
"battlefield 1 single player gameplay",
"bf1 single player gameplay",
"battlefield 1 campaign",
"bf1 campaign",
"battlefield 1 campaign gameplay",
"bf1 campaign gameplay",
"storm of steel",
"battlefield 1 storm of steel",
"bf1 storm of steel",
"prologue",
"battlefield 1 prologue"
],
"categoryId": "20",
"liveBroadcastContent": "none",
"localized": {
"title": "\u25ba STORM OF STEEL! - Battlefield 1 - 12 Minutes of Single Player Gameplay",
"description": "Battlefield 1's single player prologue, Storm of Steel! EA gave me this footage when attending a behind closed doors event at TwitchCon\n\u25ba SUBSCRIBE: http:\/\/bit.ly\/WestieSub\n\nConnect with me on other social sites:\n\n\u25ba TWITTER: http:\/\/bit.ly\/WestieTweet\n\u25ba INSTAGRAM: http:\/\/bit.ly\/InstaWestie\n\u25ba FACEBOOK: http:\/\/bit.ly\/WestieFB\n\u25ba TWITCH: http:\/\/bit.ly\/WestieTwitch\n\n\u25ba My PC Specs: http:\/\/bit.ly\/WestiePC\n\nTheWestie4321 - Westie - MrProWestie\n\n======================================\n\nWell done for reaching the bottom of the description! :D"
}
}
},
{
"kind": "youtube#video",
"etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0\/pIBDE20R99Or_-HdZL2sBavValY\"",
"id": "_8Zbs0qFPv4",
"snippet": {
"publishedAt": "2016-10-01T21:47:19.000Z",
"channelId": "UCw7FkXsC00lH2v2yB5LQoYA",
"title": "BATTLEFIELD 1 SINGLE PLAYER GAMEPLAY",
"description": "BATTLEFIELD 1 SINGLE PLAYER GAMEPLAY - 12 Minutes of footage from the Battlefield 1 Single Player campaign... Enjoy and let me know your thoughts in the comments below! Leave a rating! Sponsored by: http:\/\/scan.co.uk\/\n\n http:\/\/www.twitter.com\/jackfrags\nhttp:\/\/www.twitch.tv\/jackfrags\nhttp:\/\/www.facebook.com\/jackfragsofficial",
"thumbnails": {
"default": {
"url": "https:\/\/i.ytimg.com\/vi\/_8Zbs0qFPv4\/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https:\/\/i.ytimg.com\/vi\/_8Zbs0qFPv4\/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https:\/\/i.ytimg.com\/vi\/_8Zbs0qFPv4\/hqdefault.jpg",
"width": 480,
"height": 360
},
"standard": {
"url": "https:\/\/i.ytimg.com\/vi\/_8Zbs0qFPv4\/sddefault.jpg",
"width": 640,
"height": 480
},
"maxres": {
"url": "https:\/\/i.ytimg.com\/vi\/_8Zbs0qFPv4\/maxresdefault.jpg",
"width": 1280,
"height": 720
}
},
"channelTitle": "jackfrags",
"tags": [
"BATTLEFIELD 1 SINGLE PLAYER GAMEPLAY",
"BATTLEFIELD 1 SINGLE PLAYER",
"battlefield 1"
],
"categoryId": "20",
"liveBroadcastContent": "none",
"localized": {
"title": "BATTLEFIELD 1 SINGLE PLAYER GAMEPLAY",
"description": "BATTLEFIELD 1 SINGLE PLAYER GAMEPLAY - 12 Minutes of footage from the Battlefield 1 Single Player campaign... Enjoy and let me know your thoughts in the comments below! Leave a rating! Sponsored by: http:\/\/scan.co.uk\/\n\n http:\/\/www.twitter.com\/jackfrags\nhttp:\/\/www.twitch.tv\/jackfrags\nhttp:\/\/www.facebook.com\/jackfragsofficial"
},
"defaultAudioLanguage": "en-GB"
}
},
如何停止合并压缩阵列并将它们加在一起?
编辑:我的最终目标是让我能够使用while ($i < 150)
(来自一个数组中的所有项目,而不是使用while ($i < 50)
三次
答案 0 :(得分:1)
我在代码中没有看到任何内容......但这可以完成工作:
$result = [];
for($i = 0; $i < 50; $i++){
for($j = 1; $j <= 3; $j++){
$var = 'tags'.($j>1?$j:'');
$result[] = $$var;
}
}
json_encode($result, JSON_PRETTY_PRINT);
这会将$tags
添加到$result
然后$tags2
再添加$tags3
再添加50次
答案 1 :(得分:0)
我真的不喜欢回答我自己的问题,特别是对于这样一个简单的错误,但我遇到的问题是我误解了我想要做的事情。当@MarekJanoud让我清楚的时候,它允许我做更多的研究,发现array_merge_recursive
允许我将所有数组合并为一个允许我使用数据而我原本想要的。 / p>
谢谢@MarekJanoud