如何使用PHP从特定的JSON数据行获取数据?

时间:2015-09-01 16:01:49

标签: php arrays json

我有一个如下所示的JSON数据文件:

    {
    "aaData": [
["1","<a href=\"ra-no-1.php\">An Act Appropriating Funds for the Operation of the Government of the Commonwealth of the Philippines Beginning July First, Nineteen Hundred and Forty-Six Until the General Appropriations Act for the Fiscal Year Nineteen Hundred and Forty-Seven is Approved</a>","1946-07-15"],
["2","<a href=\"ra-no-2.php\">An Act Appropriating Fifty Thousand Pesos to Defray the Expenses of a State Funeral for Manuel L. Quezon and for the Erection of a Mausoleum to Contain His Remains</a>","1946-07-19"],
["3","<a href=\"ra-no-3.php\">An Act to Continue in Force and Effect the Act of the Congress of the United States, Approved on August 5, 1909, Entitled “An Act to Raise Revenue for the Philippine Islands, and for Other Purposes,” Otherwise Known as “The Philippine Tariff Law of 1909,” as Amended</a>","1946-07-19"],
["4","<a href=\"ra-no-4.php\">An Act to Amend Section Twenty-Six Hundred and Ninety-Two of the Revised Administrative Code, and to Exempt from Responsibility Those Who Should Surrender Firearms Under Certain Conditions, and for Other Purposes</a>","1946-07-19"],
["5","<a href=\"ra-no-5.php\">An Act to Amend Sections Two and Five of Commonwealth Act Numbered Five Hundred Eighteen, Entitled “An Act to Establish the National Coconut Corporation, and to Appropriate Additional Operating Capital for Said Corporation”</a>","1946-08-01"],
["6","<a href=\"ra-no-6.php\">An Act to Provide That as of the Date of the Proclamation of the Republic of the Philippines the Present Congress of the Philippines Shall be Known as the First Congress of the Republic of the Philippines, and for Other Purposes</a>","1946-08-05"],
["7","<a href=\"ra-no-7.php\">An Act to Establish the Foreign Funds Control Office, and for Other Purposes</a>","1946-08-09"],
["8","<a href=\"ra-no-8.php\">An Act to Authorize the President of the Philippines to Enter Into Such Contracts or Undertakings as May be Necessary to Effectuate the Transfer to the Republic of the Philippines Under the Philippine Property Act of Nineteen Hundred and Forty-Six of Any Property or Property Rights or the Proceeds Thereof Authorized to be Transferred Under Said Act; Providing for the Administration and Disposition of Such Properties Once Received; and Appropriating the Necessary Funds Therefore</a>","1946-08-09"],
["9","<a href=\"ra-no-9.php\">An Act to Authorize the President of the Philippines to Enter Into an Agreement or Agreements with the Government of the United States Pursuant to United States Public Act Numbered Four Hundred and Fifty-Four, Commonly Called the “Republic of the Philippines Military Assistance Act,” and to Issue the Necessary Rules and Regulations to Implement Said Act, and Providing Penalties for Violations Thereof</a>","1946-09-02"],
["10","<a href=\"ra-no-10.php\">An Act Penalizing Usurpation of Public Authority</a>","1946-09-02"],
["11","<a href=\"ra-no-11.php\">An Act to Prohibit the Slaughtering of Male and Female Carabaos, Horses, Mares, and Cows</a>","1946-09-02"],
["12","<a href=\"ra-no-12.php\">An Act Amending Articles One Hundred Forty-Six, Two Hundred Ninety-Five, Two Hundred Ninety-Six and Three Hundred Six of the Revised Penal Code</a>","1946-09-05"],
["13","<a href=\"ra-no-13.php\">An Act to Amend Sections Five and Six of Commonwealth Act Numbered Six Hundred and Seventy-Two, Entitled “An Act to Rehabilitate the Philippine National Bank”</a>","1946-09-05"]
    ]
}

我试图通过指定特定行并将行数据映射到特定变量来创建从文件中获取数据的标准方法。我想通过将JSON转换为数组来实现这一目的,但我发现很难理解如何选择特定行然后将行的数据映射到变量中。

最终,我想通过包含和回显/打印结果从另一个PHP文件中调用此函数。我认为我的代码看起来像这样:

<?php echo '<a href="' . $link . '" ' . 'title="' . $title . '">' ?>

我的数据将按如下方式映射:

col1 => row specifier
col2 => link
col3 => title

我希望我已正确解释了我的问题。我没有特别精通适当的词汇来解释这个问题。提前致谢! :)

2 个答案:

答案 0 :(得分:0)

使用json_decode转换为php数组

$dtaarray = json_decode(<jsonVar>,true);

http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:0)

You first need to json_decode() the JSON data into a native PHP object. The data contains an object ({...}) with a member aaData that contains an array ([...]), so you need to access the data as $data->aaData[$row].

I notice that your link data contains the entire <a> element and not just the link. If you want to add a title attribute to the <a> element, you would have to extract the link from it and then reassemble the <a> element with the title added. A much easier way would be to wrap the <a> element inside a <span> with the appropriate title attribute:

<?php

  $json_data = '
    {
      "aaData": [
        ["1","<a href=\"ra-no-1.php\">An Act Appropriating Funds for the Operation of the Government of the Commonwealth of the Philippines Beginning July First, Nineteen Hundred and Forty-Six Until the General Appropriations Act for the Fiscal Year Nineteen Hundred and Forty-Seven is Approved</a>","1946-07-15"],
        ["2","<a href=\"ra-no-2.php\">An Act Appropriating Fifty Thousand Pesos to Defray the Expenses of a State Funeral for Manuel L. Quezon and for the Erection of a Mausoleum to Contain His Remains</a>","1946-07-19"],
        ["3","<a href=\"ra-no-3.php\">An Act to Continue in Force and Effect the Act of the Congress of the United States, Approved on August 5, 1909, Entitled “An Act to Raise Revenue for the Philippine Islands, and for Other Purposes,” Otherwise Known as “The Philippine Tariff Law of 1909,” as Amended</a>","1946-07-19"],
        ["4","<a href=\"ra-no-4.php\">An Act to Amend Section Twenty-Six Hundred and Ninety-Two of the Revised Administrative Code, and to Exempt from Responsibility Those Who Should Surrender Firearms Under Certain Conditions, and for Other Purposes</a>","1946-07-19"],
        ["5","<a href=\"ra-no-5.php\">An Act to Amend Sections Two and Five of Commonwealth Act Numbered Five Hundred Eighteen, Entitled “An Act to Establish the National Coconut Corporation, and to Appropriate Additional Operating Capital for Said Corporation”</a>","1946-08-01"],
        ["6","<a href=\"ra-no-6.php\">An Act to Provide That as of the Date of the Proclamation of the Republic of the Philippines the Present Congress of the Philippines Shall be Known as the First Congress of the Republic of the Philippines, and for Other Purposes</a>","1946-08-05"],
        ["7","<a href=\"ra-no-7.php\">An Act to Establish the Foreign Funds Control Office, and for Other Purposes</a>","1946-08-09"],
        ["8","<a href=\"ra-no-8.php\">An Act to Authorize the President of the Philippines to Enter Into Such Contracts or Undertakings as May be Necessary to Effectuate the Transfer to the Republic of the Philippines Under the Philippine Property Act of Nineteen Hundred and Forty-Six of Any Property or Property Rights or the Proceeds Thereof Authorized to be Transferred Under Said Act; Providing for the Administration and Disposition of Such Properties Once Received; and Appropriating the Necessary Funds Therefore</a>","1946-08-09"],
        ["9","<a href=\"ra-no-9.php\">An Act to Authorize the President of the Philippines to Enter Into an Agreement or Agreements with the Government of the United States Pursuant to United States Public Act Numbered Four Hundred and Fifty-Four, Commonly Called the “Republic of the Philippines Military Assistance Act,” and to Issue the Necessary Rules and Regulations to Implement Said Act, and Providing Penalties for Violations Thereof</a>","1946-09-02"],
        ["10","<a href=\"ra-no-10.php\">An Act Penalizing Usurpation of Public Authority</a>","1946-09-02"],
        ["11","<a href=\"ra-no-11.php\">An Act to Prohibit the Slaughtering of Male and Female Carabaos, Horses, Mares, and Cows</a>","1946-09-02"],
        ["12","<a href=\"ra-no-12.php\">An Act Amending Articles One Hundred Forty-Six, Two Hundred Ninety-Five, Two Hundred Ninety-Six and Three Hundred Six of the Revised Penal Code</a>","1946-09-05"],
        ["13","<a href=\"ra-no-13.php\">An Act to Amend Sections Five and Six of Commonwealth Act Numbered Six Hundred and Seventy-Two, Entitled “An Act to Rehabilitate the Philippine National Bank”</a>","1946-09-05"]
      ]
    }
  ';

  $data = json_decode ($json_data);
  if (!$data) {
    die ("Failed to decode JSON data");
  }

  /* Remap data so that $links is indexed by row specifier */
  foreach ($data->aaData as $row) {
    $links[$row[0]] = array ($row[1], $row[2]);
  }

  function get_row ($row)
  {
    global $links;
    if (isset ($links[$row])) {
      return $links[$row];
    } else {
      return NULL;
    }
  }

  list ($link, $title) = get_row (5);

  if (isset ($link)) {
    echo "<span title=\"$title\">$link</span>\n";
  } else {
    echo "Row not found.\n";
  }

I'm assuming here that the href attribute and the title data have been properly encoded using htmlspecialchars() or similar.