如何从 PHP 或 smarty 中输出的单个变量创建多个变量?

How to create multiple variables from single variables output in PHP or smarty?

php代码。

    public function GetProduct($id, $bc = '')
{
    $productname = WHMCS\Product\Product::getProductName($id);
    $description = WHMCS\Product\Product::getProductDescription($id);
    $details = WHMCS\Product\Product::find($id);
    $pricing = $details->pricing();
    if ($bc == '') {
        $price = $pricing->first();
    } else {
        $price = $pricing->byCycle($bc);
        if (is_null($price)) {
            $price = $pricing->first();
        }
    }
    return array(
        'name' => $productname,
        'description' => $description,
        'price' => $price,
    );
}

我在 tpl 文件中使用 {$description} 变量时的输出。

Disk Space: 1000 MB
Bandwidth: 10,000 MB
Email Accounts: 25
Subdomains: 100
MySql DataBase: UNLIMITED

This is test information for products

Screenshot from phpMyAdmin

我想从此输出创建多个变量以获得以下结果,这样我就可以在 tpl 文件中使用以下 3 个变量。

$feature =  Disk Space
$value = 100 MB 

etc..

and also 
$feature.description = This is test information for products.

如果这是一个不清楚的问题,请告诉我,我会尽力解决。

谢谢

我相信这可以做得更干净,但它会完成工作:

$description = "Disk Space: 1000 MB
Bandwidth: 10,000 MB
Email Accounts: 25
Subdomains: 100
MySql DataBase: UNLIMITED

This is test information for products,
with a multiline description";

$descArr = explode("\r\n", $description);
$lineCount = 1;
$desc = "";
foreach ($descArr as $line)
{
    if ($lineCount <= 5)
    {
        list($key, $val) = explode(":", $line);
        $val = trim($val);
        echo "key: [" . $key . "] \n";
        echo "val: [" . $val . "] \n\n";
    }
    else
    {
        $desc .= $line . "\n";
    }
    $lineCount++;
}

echo "desc:" . $desc . "\n";

输出:

key: [Disk Space]
val: [1000 MB]

key: [Bandwidth]
val: [10,000 MB]

key: [Email Accounts]
val: [25]

key: [Subdomains]
val: [100]

key: [MySql DataBase]
val: [UNLIMITED]

desc:
This is test information for products,
with a multiline description

我刚刚 echo 找出了 key/value 对。如果你愿意,你当然可以将它们存储到变量中。


如果你想 key/val 对存储在变量中:

$description = "Disk Space: 1000 MB
Bandwidth: 10,000 MB
Email Accounts: 25
Subdomains: 100
MySql DataBase: UNLIMITED

This is test information for products,
with a multiline description";

$descArr = explode("\r\n", $description);

$keys = $vals = [];
$lineCount = 0;
$desc = "";

foreach ($descArr as $line)
{
    if ($lineCount < 5)
    {
        list($key, $val) = explode(":", $line);
        $val = trim($val);
        $keys[$lineCount] = $key;
        $vals[$lineCount] = $val;
    }
    else
    {
        $desc .= $line . "\n";
    }
    $lineCount++;
}

var_dump($keys); 
var_dump($vals); 
echo "desc:" . $desc . "\n";

输出:

array(5) {
  [0]=>
  string(10) "Disk Space"
  [1]=>
  string(9) "Bandwidth"
  [2]=>
  string(14) "Email Accounts"
  [3]=>
  string(10) "Subdomains"
  [4]=>
  string(14) "MySql DataBase"
}
array(5) {
  [0]=>
  string(7) "1000 MB"
  [1]=>
  string(9) "10,000 MB"
  [2]=>
  string(2) "25"
  [3]=>
  string(3) "100"
  [4]=>
  string(9) "UNLIMITED"
}
desc:
This is test information for products,
with a multiline description

$keys & $vals 是平行数组。这一切都基于 $description 顶部的五行格式,其中 key:value 是格式。

如果结果字符串与您的问题中的相同,您可以简单地用新行分隔它们,然后用 : 展开。这是代码。

$str = 'Disk Space: 1000 MB
Bandwidth: 10,000 MB
Email Accounts: 25
Subdomains: 100
MySql DataBase: UNLIMITED';

$arr = preg_split('/\r\n|\r|\n/', $str);
$new = array();
foreach($arr as $key=>$a){
    $temp = explode(':', $a);
    $new[$key]['feature'] = trim($temp[0]);
    $new[$key]['value'] = trim($temp[1]);
}
echo '<pre>';
print_r($arr);
echo '<br>';
print_r($new);

输出:

Array
    (
    [0] => Disk Space: 1000 MB
    [1] => Bandwidth: 10,000 MB
    [2] => Email Accounts: 25
    [3] => Subdomains: 100
    [4] => MySql DataBase: UNLIMITED
)

Array
    (
    [0] => Array
    (
        [feature] => Disk Space
        [value] => 1000 MB
    )

    [1] => Array
    (
        [feature] => Bandwidth
        [value] => 10,000 MB
    )

    [2] => Array
    (
        [feature] => Email Accounts
        [value] => 25
    )

    [3] => Array
    (
        [feature] => Subdomains
        [value] => 100
    )

    [4] => Array
    (
        [feature] => MySql DataBase
        [value] => UNLIMITED
    )
)