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

时间:2019-09-16 14:08:13

标签: php smarty whmcs

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.

如果这个问题不清楚,请告诉我,我将尝试清除它。

谢谢

2 个答案:

答案 0 :(得分:0)

我敢肯定这可以做得更干净,但可以完成工作:

list_c = [(a * 2.4) * (b * 0.5) for a, b in zip(list_a, list_b)]

输出:

$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

输出:

$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顶部五行的格式,其中$descriptionkey是格式。

答案 1 :(得分:0)

如果结果字符串与您的问题相同,则只需将它们用换行符分隔,然后用:爆炸即可。这是代码。

$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
    )
)