PHP字符串到变量

时间:2016-03-09 13:52:03

标签: php

我有

$string = "temp";

我想要提取字符串值并创建新变量(数组):

$temp = array();

可能吗?

3 个答案:

答案 0 :(得分:2)

当然可以!

${'box_'.$i.'_image};

然后你可以这样称呼:$ temp;

更多信息:http://php.net/manual/en/language.variables.variable.php

使用此方法,您可以在for循环中使用它来立即创建一堆变量:)

为例: // List of packages not to kill List<String> reservedPackages = new ArrayList<String>(); reservedPackages.add("system"); reservedPackages.add("com.android.launcher2"); reservedPackages.add("com.android.inputmethod.latin"); reservedPackages.add("com.android.phone"); reservedPackages.add("com.android.wallpaper"); reservedPackages.add("com.google.process.gapps"); reservedPackages.add("android.process.acore"); reservedPackages.add("android.process.media"); //kill all except that in the list above int processKillCounter = 0; ActivityManager am = (ActivityManager) this.getBaseContext().getSystemService(Activity.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> listeProcessus = am.getRunningAppProcesses(); for(ActivityManager.RunningAppProcessInfo processus : listeProcessus) { String packageName = processus.processName.split(":")[0]; if (!this.getBaseContext().getPackageName().equals(packageName) && !reservedPackages.contains(packageName)) { am.restartPackage(packageName); processKillCounter++; } } mi = new ActivityManager.MemoryInfo(); activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); long afterBoostMemory = mi.availMem / 1048576L; long boostedMemory = afterBoostMemory - availableMegs; //Percentage can be calculated for API 16+ long percentAvail = mi.availMem / mi.totalMem; Toast.makeText(this,"Phone Boosted, "+boostedMemory + " MB freed", Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

您可以使用Variable variables

示例:

$string = "temp";
$$string = array();
var_dump($temp);
var_dump($$string);//Same as var_dump($temp)

但是我发现这会导致难以阅读的代码,通常还有其他方法可以满足您的需求。

答案 2 :(得分:0)

您可以使用Variable variables

$string = "temp";
$$string = array();

http://php.net/manual/en/language.variables.variable.php的更多信息。