drvc63490 2015-06-03 08:32
浏览 26
已采纳

PHP文件为软DB?

I want to use a PHP include file for a soft DB of about 20 elements. The actual data will be collected from RRD files and thus I prefer not to use MySQL just for 20 elements. The file fruits.php for the elements I'm trying something like:

<?php
$fruit="Apple"; $colour="red";
$fruit="Banana"; $colour="yellow";
$fruit="Pear"; $colour="green";
?>

In my recall file, results.php, I currenlty have the following code to recall the information from the fruits.php file:

<?php
include 'fruits.php';
$num=count($fruit);
$i = 0;
while ($i < $num){
echo "<p>My ".$fruit." is ".$colour.".</p>";
$i++;
}
?>

Since I'm not a proper scriptor/programmer, I'm quite blind to what should be different and stupid to proper coding. I have tried some other methods as well, one included:

$num=glob($fruit);

Any assistance with this will be really appreciated. Just to confirm the output I'd like for this code is to be:

My Apple is red.
My Banana is yellow.
My Pear is green.
  • 写回答

2条回答 默认 最新

  • douzongmu2543 2015-06-03 08:46
    关注

    in your case, variable $fruit will have a value "pear", and $colour = "green" Because you always redefine it

     <?php
     $fruits = array();
     $fruits[] = ["name" => "Apple", "colour"=>"red"];
     $fruits[] = ["name" => "Banana", "colour"=>"yellow"];
     $fruits[] = ["name" => "Pear", "colour"=>"green"];
     ?>
    

    1st way:

    <?php
    include 'fruits.php';
    $num=count($fruits);
    $i = 0;
    while ($i < $num){
    echo "<p>My ".$fruits[$i]["name"]." is ".$fruits[$i]["colour"].".</p>";
    $i++;
    }
    ?>
    

    2nd way (much easier):

    <?php 
    include 'fruits.php';
    foreach ($fruits as $fruit){
      echo "<p>My ".$fruit["name"]." is ".$fruit["colour"].".</p>";
    }
    
    ?>
    

    where $fruit is element of the array $fruits

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

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记