PHP显示不断变化的变量


php display variable nummer that constantly changes

目前我将数据存储在MYSQL数据库中,该数据库包含1个字符串,存储在名为"data"的表中,如下所示:

parameter.troops.barracks1.3.type_index=x

现在我需要回显x的值它可以是任何值那么在PHP中如何做到这一点呢?

我的猜测是,首先我必须检查字符串是否存在,然后如果它存在,它将获取X是什么并显示它

既然一切都在字符串中,我建议:

$string = 'parameter.troops.barracks1.3.type_index=x';
$val = substr($string, strrpos($string, '=')+1);

有一种方法:

$result = trim(strchr($string, '='), '=');

这将把名称中的句点.转换为下划线_:

parse_str($string, $result);
print_r($result);

收益率:

Array
(
    [parameter_troops_barracks1_3_type_index] => x
)

所以你可以这样做:

echo $result['[parameter_troops_barracks1_3_type_index'];
//or
echo current($result);