首页 > 解决方案 > 如何用php中的变量替换url值?并在获取数组中获得动态结果?

问题描述

我想让数字“12345”和日期“02-07-2018”动态从文本框中获取它的变量值..我也想在数组中获取结果(动态)..像mysql_fetch_array(结果)..如何实现这个??..任何帮助都会得到

    <?php
    $json = '';
    //url

    $url = 'http://api.website.com/account/12345/date/02-07-2018/apikey/api_key/';
    $content = file_get_contents($url);
    $json = json_decode($content, true);


    $acc_name = $json['account']['name'];
    $acc_number = $json['account']['number'];

    ?>

    <table width='100%' border=1>

      <thead>
      <tr bgcolor='#00ffcc'>

        <th>account name</th>
        <th>account number</th>

      </tr>
      </thead>

      <tbody>

<td> <?php echo $acc_name; ?></td>
<td><?php echo $acc_number; ?></td>

标签: phparraysjsontextbox

解决方案


我希望这是您检索所需要的。

$number = $_POST['number'] ;
$date = $_POST['date'] ; /* format if necessary */

$url = "http://api.website.com/account/$number/date/$date/apikey/api_key/";

要迭代,您可以使用

<?php
foreach( $json['account'] as $value ) {
    $acc_name = $value['name'];
    $acc_number = $value['number'];
?>

<td> <?php echo $acc_name; ?></td>
<td><?php echo $acc_number; ?></td>
<?php } ?>

推荐阅读