dtuqxb3884 2015-09-01 16:01
浏览 61
已采纳

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

I have a JSON data file that looks like this:

    {
    "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"]
    ]
}

I'm trying to create a standard way to get data from the file just by specifying a particular line and mapping the row data into particular variables. I imagine that there's a way to do this using by converting the JSON into an array but I'm finding it difficult to understand how to select a particular row and then mapping the row's data into variables.

Ultimately, I want to call this function from another PHP file via includes and echoing/printing the result. I think my code would look like this:

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

and my data would be mapped as follows:

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

I hope I've explained my question properly. I'm not particularly well-versed in the proper vocabulary to explain this problem. Thanks in advance! :)

  • 写回答

2条回答 默认 最新

  • dongren1011 2015-09-01 21:46
    关注

    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>
    ";
      } else {
        echo "Row not found.
    ";
      }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址