如何在PHP中将两个JSON对象合并为一个?

时间:2014-11-12 20:51:19

标签: php json

如何将两个json对象合并为一个?两个json对象都有类似的结构 我尝试使用下面的代码,但它现在正在工作

<?php
$requestUrl="http://www.reddit.com/r/deals.json?limit=10&t=hour";
$requestUrl1="http://www.reddit.com/r/redditdeals.json?limit=10&t=hour";
$data=file_get_contents($requestUrl);
$data1=file_get_contents($requestUrl1);
$array1 = json_decode($data);
$array2 = json_decode($data1);
$json = json_encode(array_combine( $array1, $array2 )); //line 8
echo $json;
?>

编辑:抱歉,添加错误详情

警告:array_combine()要求参数1为数组,对象在&#34; URL&#34;在第8行 空

编辑:添加JSON对象$ data和$ data1(为了保持简单,我只为对象添加了一个项目)

$data = '{
  "kind": "Listing",
  "data": {
    "modhash": "ynp0pgdyf6bd03faef14e60cf6ab97f2e396bb81545ea57578",
    "children": [
      {
        "kind": "t3",
        "data": {
          "domain": "self.deals",
          "banned_by": null,
          "media_embed": {

          },
          "subreddit": "deals",
          "selftext_html": "&lt;!-- SC_OFF --&gt;&lt;div class=\"md\"&gt;&lt;p&gt;Some of you may have seen this already, but CreditSesame is running a promo for the month of November where you refer 6 friends and receive a $150 Amazon Gift Card. I&amp;#39;m hoping we can get a legitimate referral train started so we can all make some easy money. Should come in handy for Christmas shopping.&lt;\/p&gt;\n\n&lt;p&gt;THIS OFFER IS FOR THE US ONLY &amp;amp; YOU HAVE TO BE AT LEAST 18 YEARS OLD.&lt;\/p&gt;\n\n&lt;p&gt;What is CreditSesame?&lt;\/p&gt;\n\n&lt;p&gt;It&amp;#39;s pretty much just like CreditKarma. It shows you your credit score, why you got the score and what you can do to improve it.&lt;\/p&gt;\n\n&lt;p&gt;Most importantly, there is NO CREDIT CARD NEEDED to sign up because it&amp;#39;s FREE. You can of course buy some advanced features, but that&amp;#39;s not necessary.&lt;\/p&gt;\n\n&lt;p&gt;Now, you do have to provide legitimate information, as it uses your SSN to get your credit report. They pull your credit report in a way that won&amp;#39;t hurt your credit score according to their site. (Someone may be able to better explain this than me)&lt;\/p&gt;\n\n&lt;p&gt;Referral Train&lt;\/p&gt;\n\n&lt;p&gt;Here are some basic guidelines to try to get this thing running smoothly: Once you sign up, you can get your referral link by hovering over your name on in the top-right corner and selecting &amp;quot;Invite a friend&amp;quot; from the menu. Paste that link as a reply to who you signed up under in this thread. Each person only needs 6 referrals, so don&amp;#39;t keep signing up under someone when they already have 6 replies. Keep &amp;quot;forking&amp;quot; and spreading the referrals around so that everyone has at least a chance to get a few. Please remove your referral link once you receive 6 referrals so everyone knows you have completed yours. So to get this started, here is my referral link and a non-ref link. If you would like to sign up under me, I would greatly appreciate it.&lt;\/p&gt;\n\n&lt;p&gt;&lt;a href=\"http:\/\/csesa.me\/?r=3RQho7mRN\"&gt;http:\/\/csesa.me\/?r=3RQho7mRN&lt;\/a&gt; (my link)&lt;\/p&gt;\n\n&lt;p&gt;&lt;a href=\"http:\/\/www.creditsesame.com\/\"&gt;http:\/\/www.creditsesame.com\/&lt;\/a&gt; (Non-ref)&lt;\/p&gt;\n\n&lt;p&gt;Mods, I have read over the rules and I don&amp;#39;t think I&amp;#39;m breaking any. Please let me know if I am or delete the thread if this is not allowed. Thanks!&lt;\/p&gt;\n&lt;\/div&gt;&lt;!-- SC_ON --&gt;",
          "selftext": "Some of you may have seen this already, but CreditSesame is running a promo for the month of November where you refer 6 friends and receive a $150 Amazon Gift Card. I'm hoping we can get a legitimate referral train started so we can all make some easy money. Should come in handy for Christmas shopping.\n\nTHIS OFFER IS FOR THE US ONLY &amp;amp; YOU HAVE TO BE AT LEAST 18 YEARS OLD.\n\nWhat is CreditSesame?\n\nIt's pretty much just like CreditKarma. It shows you your credit score, why you got the score and what you can do to improve it.\n\nMost importantly, there is NO CREDIT CARD NEEDED to sign up because it's FREE. You can of course buy some advanced features, but that's not necessary.\n\nNow, you do have to provide legitimate information, as it uses your SSN to get your credit report. They pull your credit report in a way that won't hurt your credit score according to their site. (Someone may be able to better explain this than me)\n\nReferral Train\n\nHere are some basic guidelines to try to get this thing running smoothly: Once you sign up, you can get your referral link by hovering over your name on in the top-right corner and selecting \"Invite a friend\" from the menu. Paste that link as a reply to who you signed up under in this thread. Each person only needs 6 referrals, so don't keep signing up under someone when they already have 6 replies. Keep \"forking\" and spreading the referrals around so that everyone has at least a chance to get a few. Please remove your referral link once you receive 6 referrals so everyone knows you have completed yours. So to get this started, here is my referral link and a non-ref link. If you would like to sign up under me, I would greatly appreciate it.\n\nhttp:\/\/csesa.me\/?r=3RQho7mRN (my link)\n\nhttp:\/\/www.creditsesame.com\/ (Non-ref)\n\nMods, I have read over the rules and I don't think I'm breaking any. Please let me know if I am or delete the thread if this is not allowed. Thanks!",
          "likes": null,
          "user_reports": [

          ],
          "secure_media": null,
          "link_flair_text": null,
          "id": "2m366z",
          "gilded": 0,
          "secure_media_embed": {

          },
          "clicked": false,
          "report_reasons": null,
          "author": "EarthBrown",
          "media": null,
          "score": 5,
          "approved_by": null,
          "over_18": false,
          "hidden": false,
          "thumbnail": "self",
          "subreddit_id": "t5_2qir9",
          "edited": false,
          "link_flair_css_class": null,
          "author_flair_css_class": null,
          "downs": 0,
          "mod_reports": [

          ],
          "saved": false,
          "is_self": true,
          "name": "t3_2m366z",
          "permalink": "\/r\/deals\/comments\/2m366z\/free_150_for_6_referrals_to_credit_sesame\/",
          "stickied": false,
          "created": 1415840944,
          "url": "http:\/\/www.reddit.com\/r\/deals\/comments\/2m366z\/free_150_for_6_referrals_to_credit_sesame\/",
          "author_flair_text": null,
          "title": "Free $150 for 6 Referrals to Credit Sesame!",
          "created_utc": 1415812144,
          "ups": 5,
          "num_comments": 2,
          "visited": false,
          "num_reports": null,
          "distinguished": null
        }
      }],
    "after": "t3_2lx2yh",
    "before": null
  }
}';

$data1 = '{
  "kind": "Listing",
  "data": {
    "modhash": "mxgmpcysk5ce5f6dbb57adcc895d089f0fc5c98d08361beb37",
    "children": [
      {
        "kind": "t3",
        "data": {
          "domain": "rodneybrooksjr.com",
          "banned_by": null,
          "media_embed": {

          },
          "subreddit": "redditdeals",          
          "secure_media": null,
          "link_flair_text": null,
          "id": "2d5ad3",
          "gilded": 0,
          "secure_media_embed": {

          },
          "clicked": false,
          "report_reasons": null,
          "author": "rodneybrooksjr",
          "media": null,
          "score": 0,
          "approved_by": null,
          "over_18": false,
          "hidden": false,
          "thumbnail": "",
          "subreddit_id": "t5_2r0ax",
          "edited": false,
          "link_flair_css_class": null,
          "author_flair_css_class": null,
          "downs": 0,
          "mod_reports": [

          ],
          "saved": false,
          "is_self": false,
          "name": "t3_2d5ad3",
          "permalink": "\/r\/redditdeals\/comments\/2d5ad3\/heres_a_product_i_like_that_you_might_want_to\/",
          "stickied": false,
          "created": 1407682429,
          "url": "http:\/\/rodneybrooksjr.com\/kingston",
          "author_flair_text": null,
          "title": "Here's a product I like that you might want to check out! You can get Kingston 4GB USB Flash Drive for just $9.97 (a 44% savings!)",
          "created_utc": 1407678829,
          "ups": 0,
          "num_comments": 1,
          "visited": false,
          "num_reports": null,
          "distinguished": null
        }
      }],
    "after": "t3_1zqtxs",
    "before": null
  }
}';

我做错了吗?提前致谢

1 个答案:

答案 0 :(得分:3)

而不是array_combine将array1中的值组合为键,而将array2中的值组合为值,您应该使用array_merge($array1, $array2)来合并两个数组