dongzhe3573 2019-05-07 00:04
浏览 56

数据通过循环读取和输出

I make an event list with a maximum of 4 events, saved in its own jason files.

I wrote this code for the output.

JSON:

{"field1":"new york","field2":"1553404200","field3":1554415200}

CODE:

$jsonA = file_get_contents('code/data1.json');
$fieldsA = json_decode($jsonA, true);
$cityA = $fieldsA["field1"]; 
$dateStartA = $fieldsA["field2"];
$dateEndA = $fieldsA["field3"];

$jsonB = file_get_contents('code/data2.json');
$fieldsB = json_decode($jsonB, true);
$cityB = $fieldsB["field1"]; 
$dateStartB = $fieldsB["field2"];
$dateEndB = $fieldsB["field3"];

<div><?php echo $cityA . $dateStartA . " - " . $dateEndA?></div>
<div><?php echo $cityB . $dateStartB . " - " . $dateEndB?></div>

The problem is:

I have 4 events with each start and end date. So I have to do the same 8 times, but I do not want and should copy this code 8 times with a higher variable name... In my project I have 13 fields and also 2 arrays to output the day and month name in my language. That's why it's so important not to copy everything 8 times. I need a loop, but I do not know exactly how. I'm an absolute beginner, so please as simple as possible.

  • 写回答

1条回答 默认 最新

  • doubi8512 2019-05-07 03:33
    关注

    Great job with your attempt!

    • You might just want to write a foreach and read your files and store it in an array.

    • In your local server, you might just want to find the absolute path to your code folder. If you open your terminal, and <kbd>CTRL</kbd>+<kbd>C</kbd> code directory and <kbd>CTRL</kbd>+<kbd>V</kbd> in your terminal, you might see what your absolute path may look like. Then, paste upto before code directory in $server_path. Then, run the code, hopefully it would work.


    function searchFilenames($array, $re)
    {
        return preg_grep('/' . preg_quote($re, '/') . '/i', $array);
    }
    
    $server_path = '/path/to/your/localserver/';
    $dir = $server_path . 'code';
    $filename_array = searchFilenames(glob($dir . "/*"), 'data');
    
    $fields = array();
    foreach ($filename_array as $key => $filename) {
        $jsonA = file_get_contents($filename);
        $values = json_decode($jsonA, true);
        $fields[$key]["city"] = $values["field1"];
        $fields[$key]["dateStart"] = $values["field2"];
        $fields[$key]["dateEnd"] = $values["field3"];
    }
    var_dump($fields);
    

    Output

    array(8) {
      [0]=>
      array(3) {
        ["city"]=>
        string(8) "new york"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [1]=>
      array(3) {
        ["city"]=>
        string(6) "boston"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [2]=>
      array(3) {
        ["city"]=>
        string(7) "chicago"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [3]=>
      array(3) {
        ["city"]=>
        string(13) "washington dc"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [4]=>
      array(3) {
        ["city"]=>
        string(5) "miami"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [5]=>
      array(3) {
        ["city"]=>
        string(10) "los angles"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [6]=>
      array(3) {
        ["city"]=>
        string(7) "seattle"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
      [7]=>
      array(3) {
        ["city"]=>
        string(6) "austin"
        ["dateStart"]=>
        string(10) "1553404200"
        ["dateEnd"]=>
        int(1554415200)
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度