I am updating my class Nesty so it's infinite but I'm having a little trouble.... Here is the class:
<?php
Class Nesty
{
    // Class Variables
    private $text;
    private $data = array();
    private $loops = 0;
    private $maxLoops = 0;
    public function __construct($text,$data = array(),$maxLoops = 5)
    {
        // Set the class vars
        $this->text = $text;
        $this->data = $data;
        $this->maxLoops = $maxLoops;
    }
    // Loop function
    private function loopThrough($data)
    {
        if( ($this->loops +1) > $this->maxLoops )
        {
            die("ERROR: Too many loops!");
        }
        else
        {
            $keys = array_keys($data);
            for($x = 0; $x < count($keys); $x++)
            {
                if(is_array($data[$keys[$x]]))
                {
                    $this->loopThrough($data[$keys[$x]]);
                }
                else
                {
                    return $data[$keys[$x]];
                }
            }
        }
    }
    // Templater method
    public function template()
    {
        echo $this->loopThrough($this->data);
    }
}
?>
Here is the code you would use to create an instance of the class:
<?php
// The nested array
$data = array(
    "person" => array(
        "name" => "Tom Arnfeld",
        "age" => 15
    ),
    "product" => array (
        "name" => "Cakes",
        "price" => array (
            "single" => 59,
            "double" => 99
        )
    ),
    "other" => "string"
);  
// Retreive the template text
$file = "TestData.tpl";
$fp = fopen($file,"r");
$text = fread($fp,filesize($file));
// Create the Nesty object
require_once('Nesty.php');
$nesty = new Nesty($text,$data);
// Save the newly templated text to a variable $message
$message = $nesty->template();
// Print o开发者_Go百科ut $message on the page
echo("<pre>".$message."</pre>");
?>
Here is a sample template file:
Dear <!--[person][name]-->,
Thanks for contacting us regarding our <!--[product][name]-->. We will try and get back to you within the next 24 hours.
Please could you reply to this email to certify you will be charged $<!--[product][price][single]--> for the product.
Thanks,
Company.
The problem is that I only seem to get "string" out on the page... :( Any ideas?
if(is_array($data[$keys[$x]]))
{
   $this->loopThrough($data[$keys[$x]]);
}
else
{
   return $data[$keys[$x]];
}
You need to return from the first if statement.
if(is_array($data[$keys[$x]]))
{
   return $this->loopThrough($data[$keys[$x]]);
}
else
{
   return $data[$keys[$x]];
}
This will get you a result back when you recurse. You're only getting "string" back right now because that key is only 1 level deep in your array structure.
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论