【问题标题】:HTML form to JSON using PHP 5.13 (multidimensional)使用 PHP 5.13(多维)将 HTML 表单转换为 JSON
【发布时间】:2018-09-18 20:03:14
【问题描述】:

我已经搜索和搜索,我已经接近了,但我仍然缺少一些东西,我希望我能在这里获得一些额外的指导。我对 PHP 和 JSON 比较陌生。 我需要使用一个 JSON 文件,以可编辑的 HTML 表单显示它,然后获取更改/提交的值并用数据覆盖现有的 JSON 表单。我已经成功地使用了 JSON 并显示了表单,但我正在努力将表单数据恢复为正确的 JSON 格式。 我受制于我被困在使用 PHP 5.1.3 的事实(是的,我知道......不是自愿的); JavaScript 不受欢迎,服务器没有任何可用的 JQuery 库。

这是 HTML:

<form action="test.php" method="POST">
     <table>
        <?php read_EDT_json ( $new_edt); ?>
        <tr><td colspan="2"><input type="submit" name="submit"></td></tr>
     </table>
</form>

函数如下:

function read_EDT_json($theJSON) {
if ( file_exists ( "files/" . $theJSON ) ) {
    $json = file_get_contents ( "files/" . $theJSON );

    $jsonIterator = new RecursiveIteratorIterator ( new RecursiveArrayIterator ( json_decode ( $json, TRUE ) ), RecursiveIteratorIterator::SELF_FIRST );

    foreach ( $jsonIterator as $key => $value) {
    if ( is_array ( $value ) ) {
      if ( !is_numeric ( $key ) ) {
         echo "<tr><td colspan='2'><strong><input type='hidden' name='league[$key]' value='" . $key . "'>" . $key . "</input></strong></td></tr>\n";
      }
        foreach ( $value as $c => $r ) {
         if ( is_array ( $r ) ) {
          foreach ( $r as $t => $l ) {
            if ( $t === "team" ) {
             echo "<tr><td><input type='hidden' name='league[$key][team]' value='" . $l . "'>" . $l . "</input></td>";
            } else {
              echo "<td><input type='text' name='league[$key][points]' value='" . $l . "'></input></td></tr>\n";
                            }
          }
        }
      }
  }
}
}

}

最后,这是帖子上的操作(我没有显示写入 JSON 部分,因为我已经知道如何做到这一点):

if( isset ( $_POST [ "league" ] ) ) {
   foreach ( $_POST [ "league" ] as $k => $v ) {
       $params [ $k ] = $v;
   }
echo json_encode ( $params ); 
}

当我检索 $_POST 时,json_encode 给了我:

{"EPL":{"team":"Watford","points":"12"},"MLS":{"team":"Montreal","points":"39"}}

我需要它看起来像:

{ "EPL": [ { "team": "Chelsea", "points": 15.0 }, { "team": "Liverpool", "points": 15.0 }, { "team": "City", "points": 13.0 }, { "team": "Waterford", "points": 12.0 }], "MLS": [ { "team": "Atlanta", "points": 57.0 }, { "team": "New York", "points": 56.0 }, { "team": "Columbus", "points": 44.0 }, { "team": "Philadelphia", "points": 40.0 }, { "team": "Montreal", "points": 39.0 }] }

注意:我只得到内部数组的最终值。我也没有在 EPL 之后和一队之前得到“[”开场白,也没有得到结束“]”或在“MLS”之前和之后开场。

对于相关 PHP/JSON 新手的任何建议将不胜感激。

干杯。

【问题讨论】:

    标签: php json


    【解决方案1】:
    if( isset ( $_POST [ "league" ] ) ) {
        foreach ( $_POST [ "league" ] as $k => $v ) {
            if(isset($params[$k])){ // has $params[$k] already been set?
                if(is_array($params[$k])){ // $params[$k] should be an array as it has multiple values. Is it?
                    array_push($params[$k],$v); // append to $params[$k]
                } else {
                    $params[$k] = array($params[$k],$v); // convert $params[$k] into an array, and append $v
                }
            } else {
                $params [ $k ] = $v;
            }
        }
        echo json_encode ( $params ); 
    }
    

    您的代码用当前的 $v 覆盖了 $params[$k],而不是将其视为一个数组。

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 2020-02-06
      • 1970-01-01
      • 2020-02-28
      • 2014-08-23
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多